When using callbacks inside a loop in javascript, is there any way to save a variable that's updated in the loop for use in the callback?

JavascriptCallback

Javascript Problem Overview


Let's say I have something as follows:

for(var i = 0; i < length; i++){
  var variable = variables[i];
  otherVariable.doSomething(variable, function(err){ //callback for when doSomething ends
    do something else with variable;
  }

By the time the callbacks are called, variable will inevitably be the last variable for all the callbacks, instead of being a different one for each callback, as I would like. I realize that I could pass variable to doSomething() and then get that passed back as part of the callback, but doSomething() is part of an external library, and I'd rather not mess around with the source code for that.

Do those of you that know JavaScript better than I do know if there are any alternative ways to do what I'd like to do?

Best, and thanks,
Sami

Javascript Solutions


Solution 1 - Javascript

A common, if ugly, way of dealing with this situation is to use another function that is immediately invoked to create a scope to hold the variable.

for(var i = 0; i < length; i++) {
  var variable = variables[i];
  otherVariable.doSomething(function(v) { return function(err) { /* something with v */ }; }(variable));
}

Notice that inside the immediately invoked function the callback that is being created, and returned, references the parameter to the function v and not the outside variable. To make this read much better I would suggest extracting the constructor of the callback as a named function.

function callbackFor(v) {
  return function(err) { /* something with v */ };
}
for(var i = 0; i < length; i++) {
  var variable = variables[i];
  otherVariable.doSomething(callbackFor(variable));
}

Solution 2 - Javascript

Ok I've seemed to figure this out. What I needed to do was to put a function(var) wrapper around otherVariable.doSomething(), so the updated code looks as follows:

for(var i = 0; i < length; i++){
  var variable = variables[i];
  (function(var){ //start wrapper code
    otherVariable.doSomething(var, function(err){ //callback for when doSomething ends
      do something else with var; //please note that i'm dealing with var here, not variable
    }
  })(variable);//passing in variable to var here
}

hope this helps anybody else that gets stuck on something like this in the future!

@aparker42, i'd still love to hear your answer to my question in the comment to your question, since that still does confuse me.

EDIT: of course, since this is javascript, you wouldn't want to use var as a variable name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthisissamiView Question on Stackoverflow
Solution 1 - Javascriptaparker42View Answer on Stackoverflow
Solution 2 - JavascriptthisissamiView Answer on Stackoverflow