Nesting node async.eachSeries

For noon, she worked with an asynchronous module, but could not get it to work properly when nesting several levels. A bit long message due to code, but please, with me, all code should work fine.

So this works fine:

     var async = require ('async')

     var myarr = ["Outer - A", "Outer - B"];
     var myarr2 = ["Inner - A", "Inner - B"];
     var innerComplete = true;

     async.eachSeries (myarr, function (item, outerCallback) {
         console.log ('Processing item' + item);
         async.series ([
             function (callback) {
                 takeTime (2000, item, callback)
             },
             function (callback) {
                 takeTime (1000, item, callback)
             },
             function (callback) {


                 outerCallback ();
             }
         ], function (err) {
             console.log ("--- OUTER SEQUENCE ---")

         })
     }, function (err) {
         console.log ("--- OUTER LOOP ---")

     });

     function takeTime (ms, msg, callback) {
         console.log ("Starting" + ms + "ms task from" + msg);
         setTimeout (function () { 
             console.log ("Finished" + ms + "ms task from" + msg);
             callback ();
         }, ms);
     } 

And he displays everything beautifully in sequence as follows:

  Processing item Outer - A
 Starting 2000 ms task from Outer - A
 Finished 2000 ms task from Outer - A
 Starting 1000 ms task from Outer - A
 Finished 1000 ms task from Outer - A
 Processing item Outer - B
 Starting 2000 ms task from Outer - B
 Finished 2000 ms task from Outer - B
 Starting 1000 ms task from Outer - B
 Finished 1000 ms task from Outer - B
 --- OUTER LOOP ---

But when I try to nest the following everySeries loop as follows:

     var async = require ('async')

     var myarr = ["Outer - A", "Outer - B"];
     var myarr2 = ["Inner - A", "Inner - B"];
     var innerComplete = true;

     async.eachSeries (myarr, function (item, outerCallback) {
         console.log ('Processing item' + item);
         async.series ([
             function (callback) {
                 takeTime (2000, item, callback)
             },
             function (callback) {
                 takeTime (1000, item, callback)
             },
             function (callback) {
                 async.eachSeries (myarr2, function (item2, outerCallback2) {
                     console.log ('Processing item' + item2);
                     async.series ([
                         function (callback2) {
                             takeTime (2000, item2, callback2)
                         },
                         function (callback2) {
                             takeTime (1000, item2, callback2)
                         }
                     ], function (err) {
                         console.log ('--- INNER SEQUENCE ---')

                     })
                 }, function (err) {
                     console.log ("--- INNER LOOP ---")
                 });

                 outerCallback ();
             }
         ], function (err) {
             console.log ("--- OUTER SEQUENCE ---")

         })
     }, function (err) {
         console.log ("--- OUTER LOOP ---")

     });

     function takeTime (ms, msg, callback) {
         console.log ("Starting" + ms + "ms task from" + msg);
         setTimeout (function () { 
             console.log ("Finished" + ms + "ms task from" + msg);
             callback ();
         }, ms);
     } 

It loses execution order when you enter the second eachSeries loop as follows:

  Processing item Outer - A
 Starting 2000 ms task from Outer - A
 Finished 2000 ms task from Outer - A
 Starting 1000 ms task from Outer - A
 Finished 1000 ms task from Outer - A
 Processing item Inner - A
 Starting 2000 ms task from Inner - A
 Processing item Outer - B
 Starting 2000 ms task from Outer - B
 Finished 2000 ms task from Inner - A
 Starting 1000 ms task from Inner - A
 Finished 2000 ms task from Outer - B
 Starting 1000 ms task from Outer - B
 Finished 1000 ms task from Inner - A
 --- INNER SEQUENCE ---
 Finished 1000 ms task from Outer - B
 Processing item Inner - A
 Starting 2000 ms task from Inner - A
 --- OUTER LOOP ---
 Finished 2000 ms task from Inner - A
 Starting 1000 ms task from Inner - A
 Finished 1000 ms task from Inner - A
 --- INNER SEQUENCE ---

I also tried a waterfall, mapSeries, etc., but mixed up the execution sequence with the same or another way. Am I doing something wrong or doesn’t support such an asynchronous module?

+5
source share
1 answer

You are not calling outerCallback2 , you are not calling callback , and you are calling outerCallback immediately.

Fixed

 async.eachSeries(myarr, function( item, outerCallback) { ,----------------------------------------' | console.log('Processing item ' + item); | async.series([ | function(callback) { | `--------------, | takeTime(2000, item, callback) | }, | function(callback) { | `--------------, | takeTime(1000, item, callback) | }, | function(callback) { | ,-----------' | | async.eachSeries(myarr2, function( item2, outerCallback2) { | | ,---------------------------------------------' | | | console.log('Processing item ' + item2); | | | async.series([ | | | function(callback2) { | | | takeTime(2000, item2, callback2) | | | }, | | | function(callback2) { | | | takeTime(1000, item2, callback2) | | | } | | | ], function(err) { | | | console.log('---INNER SEQUENCE---') | | `---> outerCallback2(err); // <<< | | }) | | }, function(err){ | | console.log("---INNER LOOP---"); | `---> callback(err); // <<< | }); | } | ], function(err) { | console.log("---OUTER SEQUENCE---") `---> outerCallback(err); // <<< }) }, function(err){ console.log("---OUTER LOOP---") console.log("everything done"); }); 
+10
source

Source: https://habr.com/ru/post/1202017/


All Articles