title: Is there a way to pass variables from beforeSend to success functions in the jQuery .ajax function?
body: <p><strong>The Problem</strong> </p>
  
  <p>When working with AJAX to query a remote API, by the asynchronous nature of the request it comes back whenever it completes. The problem is when I have to make iterative calls to the same API with different criteria, I don't know which response is coming back.</p>
  
  <p>The question: Is it possible to pass a variable from </p>
  
  <p>Sample code: (simplified)</p>
  
  <pre><code>n=5;
  for(i=0; i &lt; n; i++) {
    $.ajax({
      url: someURL,
      method: post,
      // I don't want to have to use async: false, that's bad
      // async: false,
      data: JSON.stringify(someData),
      beforeSend: function(){
        console.log("Starting request #"+i)
      },
      error: function(err, code, text) {
        alert("Something went wrong: \n\n" + code + ": " + text);
      },
      success: function(response) {
        console.log("Response for request #"+i)
        console.log(response)
      }
    })
  }
  </code></pre>
  
  <p>The problem comes up in that final success function: What I should see is:</p>
  
  <pre><code>Starting request #0
  Starting request #1
  Starting request #2
  Starting request #3
  Starting request #4
  Response for request #2
  [object]
  Response for request #1
  [object]
  Response for request #4
  [object]
  Response for request #0
  [object]
  Response for request #3
  [object]
  </code></pre>
  
  <p>What I actually see is:</p>
  
  <pre><code>Starting request #0
  Starting request #1
  Starting request #2
  Starting request #3
  Starting request #4
  Response for request #4
  [object]
  Response for request #4
  [object]
  Response for request #4
  [object]
  Response for request #4
  [object]
  Response for request #4
  [object]
  </code></pre>
  
  <p>This is important not because I care about my logs being right, but because the actual version needs to be able to reconcile the responses with what was sent. I don't want to go synchronous on this, because it'll be slow, annoying, and possibly time out.</p>