0

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!

2
  • 1
    It's not a problem with mongoose, you need to read up on closures and how to use them. Commented Sep 5, 2017 at 21:01
  • Not only is this a misunderstanding of async functions, but 9/10 times it's a misunderstanding of how to issue the database query in general. There are various features to avoid "calling async methods in a loop". A couple of them I touched on in a recent explanation. But the question though "mentioning mongoose", fails to show what you are actually doing. So instead of the abstract code, maybe you should show what you are actually attempting to do instead. Context matters. Commented Sep 6, 2017 at 2:00

1 Answer 1

0

As @Cruiser have mentioned, the problem is with closure. store.find is likely to be an asynchronous operation, so when callback is actually called, the context (or, the value of i in this case) may have already changed.

Now you can test code:

var functionOne = (callback)=>{
 console.log("this is function one")
  setTimeout(callback, 50); // delay by 50ms
  }


 for (var i = 0; i<10; i++){
 functionOne(()=>{
   console.log(i);
  })
}

The output will be something like:

this is function one (repeated 10 times)
10 (repeated 10 times)

However, if you wrap it in a closure:

var functionOne = (callback)=>{
  console.log("this is function one")
  setTimeout(callback, 50); // delay by 50ms
};

for (var i = 0; i<10; i++) {
  (function (index) {
    functionOne(() => console.log(index));
  })(i);
};

And, you get this:

this is function one (repeated 10 times)
0
1
...
8
9

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.