title: How to pass a local variable to a promise done function
body: <p>I have the following script, which runs 5 ajax requests and tells when they are done. All that using jQuery promises and <code>$.where</code>.</p>
  
  <p><a href="http://jsfiddle.net/r269j65r/" rel="nofollow">http://jsfiddle.net/r269j65r/</a></p>
  
  <pre><code>console.info('Creating promises: start');
  var p = [];
  for(var i=0; i&lt;5; i++) {
      a = $.when( x(i) ).done(function(){
          console.log('Promise ' + i + ': done');
      });
      p.push(a);
  }
  console.info('Creating promises: done');
  console.log('Running promises: start');
  
  $.when.apply($,p).done(function(){
      console.log('Running promises: done');
  });
  
  function x(i) {
      console.log('Promise ' + i + ': start');
      return $.ajax({
          'url' : '/'         
      })
      .done( function() {
      });
  }
  </code></pre>
  
  <p>The problem is that <code>console.log('Promise ' + i + ': done');</code> always returns <code>Promise 5: done</code>, because <code>i</code>-variable has value <code>5</code> when the <code>done()</code> function is executed.</p>
  
  <p>My question is what be a good and clean way to give <code>i</code> to the context of the <code>done()</code>-function in the <code>for</code>-loop?</p>