javascript - Nodo de anidamiento async.eachSeries
node.js nested (1)
No está llamando al outerCallback2
, no está llamando a la callback
, y está llamando al outerCallback
inmediatamente.
Fijo:
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");
});
Estuve peleando con el módulo asíncrono durante medio día, pero no puedo hacerlo funcionar correctamente al anidar algunos niveles. Una publicación un poco larga debido al código, pero por favor desnudo conmigo, todo el código debería funcionar bien.
Entonces esto funciona bien:
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); }
Y produce todo muy bien en una secuencia como esta:
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---
Pero cuando intento anidar otro eachSeries, bucle en él de esta manera:
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); }
Pierde el orden de ejecución al ingresar el segundo bucle de cada serie de esta manera:
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---
También probé Cascada, MapSeries, etc., pero con la misma secuencia de ejecución. ¿Estoy haciendo algo mal o el módulo asíncrono no admite dicho anidamiento?