The situation is as follows: Ajax returns an array and after that an ajax request is made for each element in this array, and for one element, another ajax request is made within one ajax request. I tried to depict it schematically.

ajax 1 success{ foreach array{ ajax 2 success { ajax 3 success { действие 1 } } ajax 4 success { действие 2 } ajax 5 success { действие 3 } } } 

It turns out that step 1 is executed at the very least, but it’s necessary that everything be done in order. How to allow execution of 2 and 3 actions only after the first one? Moreover, it may be that ajax 2 will not exist, and therefore putting 4 and 5 ajax requests into the success function ajax 3 is not an option.

  • try to install async : false - Shadow33
  • function ajax returns promise - use it. - Grundy
  • add sample code that already exists - Grundy
  • one
    @ Shadow33 This is a bad option: synchronous Ajax requests in deprecated chrome. For such things there is deferred or promise API - Mr. Brightside
  • @ilya, you’ll just make branches .then(function () { $ajax...then(function () { $ajax....then(function) {}})}) - Vasily Barbashev

3 answers 3

Use a chain of promises.

Each ajax request returns a promise. You can call the then method to perform some action at the end of the request. The trick is that the then method also returns a promise. Here is an example:

 $.ajax(...).then(function (resp0) { // вызов трех запросов: var q1 = $.ajax(...).then(function (resp1) { var q11 = $.ajax(...); return $.when(resp1, q11); }) var q2 = $.ajax(...); var a3 = $.ajax(...); // Первое действие - по завершению обоих запросов q1 var a1 = q1.then(function(resp1, resp11) { console.log(resp1, resp11); }); // Второе действие - по завершению q2 и первого действия: var a2 = $.when(q2, a1).then(function (resp2) { console.log(resp2); }); // Третье действие - по завершению q3 и первого действия: var a3 = $.when(q3, a1).then(function (resp3) { console.log(resp3); }); return $.when(a1, a2, a3); // Запрос считать успешным когда все действия завершились }).then(function (...) { // А этот блок выполнится уже после всех действий... }); 

Since in your question we talked about an array, you will probably not be so simple (well, or not so hard, here's how to look). An ordered list of primitives for working with ajax requests and promises can be found in my old answer:

JSONP sequential execution

I can’t give you any specific information, because the question is too general.

     function iterate(arr) { return arr.reduce((el,promise) => { return promise.then(result => { if(!result) { return el } else if(result instanceOf Array) { return iterate(result) } else { return result } }) }, Promise.resolve()) } iterate([$.post('/some-url')]).then(() => { //code after all requests }) 

      If you understand the logic correctly, try creating variables and assigning them a value and checking that they are equal.

      • misunderstood) - Vasily Barbashev
      • @neek and if the check is before the value from the previous ajax request was set? - alias
      • @alias ... so what? - Pavel Mayorov
      • @PavelMayorov the following requests will not be. At the time of the check, the previous request will not yet execute the corresponding variable, which will not be changed for the check. - alias