0

I have structure of my programm like this

if(some condition){
    $.ajax(...);
}
if(some condition){
    $.ajax(...);
}
doSomething();

How I can wait responce of async $.ajax functions before doSomething()? Is it "true way" to use async: false ?

user3449979
  • 439
  • 1
  • 5
  • 7
  • Setting `async: false` will almost never solve anything. If you need it, it's mostly because of bad design. The solution from A. Wolff should be the prefered way. – smoksnes Oct 21 '16 at 09:22
  • Don't ever use async:false as @smoksnes already said. You might want to take a look at [stackoverflow.com/questions/10004112/how-can-i-wait-for-set-of-asynchronous-callback-functions](http://stackoverflow.com/questions/10004112/how-can-i-wait-for-set-of-asynchronous-callback-functions) – Flyer53 Oct 21 '16 at 10:28

1 Answers1

2

You could push these requests in an array:

var requests = [];
if(some condition){
    requests.push($.ajax(...));
}
if(some condition){
    requests.push($.ajax(...));
}

$.when.apply($, requests ).done(doSomething);
A. Wolff
  • 74,033
  • 9
  • 94
  • 155