title: Javascript - how to work with the iterator in a for loop with callbacks
body: <p>I am trying in the for loop to access the value of the i with which the callback function uses.</p>
  
  <p>How can I do this?</p>
  
  <pre><code>for (var i = 0; i &lt; a.length; i++)
  {
      calcRoute(fixedLocation, my_cities[i].address, function(response) {
  
          // i want here to have the current "i" here
  
      });             
  }
  </code></pre>
  
  <p>which calls...</p>
  
  <pre><code>function calcRoute(x, y, callback) {
  
      var start = x;
      var end = y;
  
      var request = {
          origin:start,
          destination:end,
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.METRIC,
          optimizeWaypoints: true
      };
  
      directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
              callback(response);                                                                 
          } else {
              alert("City unknown.")
          }       
      }); 
  }
  </code></pre>