I'm trying to run a Mongoose call back function each time my for loop iterates.
My code looks like this:
for(var i =0; i<10; i++){
store.find({},()=>{
console.log(i);
})
}
However, when this is ran I see the number 10, printed 10 times and not what I expect which is a count from one to ten being printed through the call back inside the .find method.
What has confused me further is that if I run this code:
var functionOne = (callback)=>{
console.log("this is function one")
callback();
}
for (var i = 0; i<10; i++){
functionOne(()=>{
console.log(i);
})
}
I get the expected result which is "this is function one" being printed 10 times and then the count from one to ten being executed.
Can someone please shed some light on why my Mongoose call back is not working and yet when I do a really basic example this does?
Any help would be much appreciated as I feel like I'm missing a trick here!