Hello. I have this code here (this is the structure of my code):

for (var x = 0; x < array1.length; x++){ (function(x) { $.ajax({ url : "http://someurl1.ru/"+array1[x]+"/", async: false, success : function(someresult1){ array2.length = 0; array3.length = 0; var br = someresult1.split('<br>'); for (var b = 0; b < br.length-1; b++){ var space = br[b].split(" "); array2.push(space[0]); array3.push(space[1]); } for (var v = 0; v < array2.length; v++){ (function(v) { $.ajax({ url : "http://someurl2.ru/"+array2[v]+"_"+array3[v]+"/", async: false, success : function(someresult2){ if(JSON.stringify(someresult2).search(some_array[x]) != -1){ $.ajax({ url : "http://someurl3.ru/"+array2[v]+"/"+array3[v]+"/"+some_another_array[x]+"", async: false, success : function(someresult3){ array4.push(someresult3); } }); } } }); })(v); } } }); })(x); } 

As you can see, he performs his task in the "forehead" (as written in my other question). This is truly so. And I have a question how to remake this code according to the mind? What I need is that all my ajax requests are executed in order. Now the whole structure is held by async: false. Without it, everything does not work correctly. And with him everything is pretty good, but the interface "hangs", well, it works quite slowly. Help improve this code. I laid out the structure of the code, and not the code itself, for a simpler understanding of my problem. As he in my understanding should work:

 1. Π‘Π΅Ρ€Π΅ΠΌ элСмСнт ΠΈΠ· 1 массива. 2. Π€ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ ссылку ΠΈ отправляСм запрос. 3. ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΎΡ‚Π²Π΅Ρ‚ ΠΈ Π΄Π΅Π»Π°Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹Π΅ Π½Π°ΠΌ дСйствия (Ρ€Π°Π·Ρ€Π΅Π·Π°Π΅ΠΌ ΠΎΡ‚Π²Π΅Ρ‚ ΠΈ загоняСм Π² массивы 2 ΠΈ 3). 4. Π€ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ ссылку ΠΈ отправляСм запрос. 5. ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΎΡ‚Π²Π΅Ρ‚ ΠΈ Π΄Π΅Π»Π°Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹Π΅ Π½Π°ΠΌ дСйствия (провСряСм совпадСниС ΠΎΡ‚Π²Π΅Ρ‚Π° с элСмСнтом Π² массивС). 6. Если Π΅ΡΡ‚ΡŒ совпадСниС Ρ‚ΠΎ Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ ссылку ΠΈ отправляСм запрос. 7. ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΎΡ‚Π²Π΅Ρ‚. 8. И Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π±Π΅Ρ€Π΅ΠΌ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ элСмСнт ΠΈΠ· 1 массива. 

I read quite a lot of answers on similar questions. Instead of async: false and for loops, it is suggested to use the recursive function call from the success and jquery promise callbacks. But it is offered everywhere where they used either queries in a cycle, or you need to execute queries in turn. But I have a situation when I want to execute requests in turn (and what would they wait for each other) and it is still in the loop (+ I have requests embedded in each other). So help the experts.

  • I understand you are a site Pars? Why not on the server? - user1607046
  • He began to write this "bot" for a long time now he has grown very much and it is already quite difficult to rewrite it. Well, the point here is not even in this probably. For the time being, I’m just wondering how people with a programming skill above mine can solve this problem with nested queries in a loop. What would I see "etiquette" programming something. - el pax

1 answer 1

Instead of recursively calling callbacks, it is more convenient to use the reduce function to collapse an array into a promise, which will contain a chain of necessary operations on the elements of the array. Your code using this approach will look something like this:

 // пустой промис, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΡ€ΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΡΡ‚ΡŒΡΡ дальнСйшиС ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ var init = $.Deferred(); // массив url, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ запрос var array1 = [ ... ]; // сворачиваСм значСния array1 Π² промис, ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ запуская Π΅Π³ΠΎ Π½Π° Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ // prevValue - промис, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄Π΅ΠΌ ΠΏΡ€ΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΡΡ‚ΡŒ ΠΏΡ€ΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΠΈΡ‚ΡŒ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΡƒ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ url // curUrl - Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ элСмСнт массива // curIndex - индСкс элСмСнта var result = array1.reduce(function (prevValue, curUrl, curIndex) { return prevValue .then(function () { return $.ajax(curUrl); }) .then(function (ajaxResult) { return /* Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ ΠΈΠ· ajaxResult вмСсто Π΄Π²ΡƒΡ… массивов array2, array3 ΠΎΠ΄ΠΈΠ½ массив ΠΈΠ· ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π΅Π³ΠΎ */; }) .then(function (array23) { // сворачиваСм array23 Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π½ΠΎ array1 Π² Π½ΠΎΠ²Ρ‹ΠΉ промис // ΠΌΠΎΠΆΠ½ΠΎ вынСсти Π² ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ для читаСмости var init2 = $.Deferred().resolve(); return array23.reduce(function (prevValue2, curArray23Value) { return prevValue2 .then(function () { return $.ajax(/* url ΠΈΠ· array23 */) }) .then(function (ajaxResult2) { if (JSON.stringify(ajaxResult2).search(some_array[curIndex]) != -1) { return $.ajax(/* url ΠΈΠ· curArray23Value ΠΈ some_another_array[curIndex] */); } }) .then(function (ajaxResult3) { // Ссли Π½Π° ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰Π΅ΠΌ шагС запрос Π½Π΅ выполнялся, // ajaxResult3 Π±ΡƒΠ΄Π΅Ρ‚ undefined if (ajaxResult3) { array4.push(ajaxResult3); } }); }, init2.promise()); }); }, init.resolve().promise()); result.then(function () { // здСсь ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ array4 });