0

I have a following function.

finalResults = []
promises = []

for i in arr1
  for j in arr2

    request = $.ajax
      url:"/getData"
      data:
        userId:1
      dataType:"JSON"
      success: (results) =>
        finalResults.push
          data   : results
          index1 : i
          index2 : j

     promises.push(request)

$.when.apply(null, promises).done( (a) =>
  for i in finalResults
    console.log(i.index1, i.index2)  // these values are incorrect!
)

And it looks like passing the variables "i" and "j" are not loaded correctly to the finalResults. I can see why, because these are asynchronous requests.

How can I change this to get the correct variables, corresponding to each AJAX request?

ericbae
  • 9,604
  • 25
  • 75
  • 108

0 Answers0