title: 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?
body: <p>Let's say I have something as follows:</p>
  
  <pre><code>for(var i = 0; i &lt; length; i++){
    var variable = variables[i];
    otherVariable.doSomething(variable, function(err){ //callback for when doSomething ends
      do something else with variable;
    }
  </code></pre>
  
  <p>By the time the callbacks are called, <code>variable</code> 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 <code>variable</code> to <code>doSomething()</code> and then get that passed back as part of the callback, but <code>doSomething()</code> is part of an external library, and I'd rather not mess around with the source code for that.</p>
  
  <p>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?</p>
  
  <p>Best, and thanks,<br />Sami</p>