0

I have this code. It makes many ajax requests and store them in the requests array. Then i give $.when() my requests.

value.forEach((data) => { // value contains 5 items
    requests.push($.get('my-url/' + data.id ));
  });

  $.when(requests).then((data) => {
    data.forEach((val) => {
      console.log(val.responseJSON);
    });
  });

So, my problem is: If i log the data variable in $.when(), it shows me for my objects the responseJSON prop. But if i try to loop over my objects and access them via val.responseJSON it returns undefined.

nutzt
  • 2,773
  • 3
  • 17
  • 26
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – JJJ Jul 17 '15 at 17:43
  • I do not get it ... I thought that `then` is extra for to receive the data only after all requests? I tried to `return $.when(requests);` and access them with `done` from the called function, but no...can someone help me? :( – nutzt Jul 17 '15 at 18:04
  • `$.when` doesn't accept array try `$.when.apply(null, requests).then..` – charlietfl Jul 17 '15 at 18:16
  • @Juhana using `$.when` for array of promises is not anything like duplicate pointed to – charlietfl Jul 17 '15 at 18:17
  • ok, when i use `apply` it returns an array with 3 items. The first item is what i need from my first ajax request. The second is an string with `success` and the third item is the same what i have first on my post here (with `responseJSON` and so...). But what is with my other 2 ajax requests? :( Damn... – nutzt Jul 17 '15 at 18:33
  • Ok ^^' I use arguments.length, and i know my items are passed as arguments to my `then`. Thank you very much for your help – nutzt Jul 17 '15 at 18:38

0 Answers0