本文介绍了nodejs Async.each嵌套循环混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个嵌套的for循环

i want to have two nested for loops

async.each(ListA,
function(itemA,callback1){
        //process itemA
        async.each(itemA.Children,
        function(itemAChild,callback1){
              //process itemAChild
              callback1();
               }),
    function(err){
    console.log("InnerLoopFinished")
    }

callback();
}),function(err){
console.log("OuterLoopFinished")
}
console.log("Process Finished")

现在我希望输出像{内部循环完成外循环完成},根据列表大小和

Now i expect an output Like{InnerLoopFinishedOuterLoopFinished} according to List Size and

完成流程

我得到的是流程首先完成以及InnerLoop和Outerloop消息,具体取决于循环大小.

Bt what i get isProcess Finished at Firstand InnerLoop and Outerloop message depending upon loop size..

Im在两个循环中都处理数据,因此当控制权去打印"final process"消息时,我希望我的所有数据都被填充到一个Object之前,并作为响应发送,而这在这里没有实现

Im process data in both loops so when control goes to print "final process" message i expect all my data is populated to an Object Before that and send it as a response which isnt achieved here

我认为我不太清楚异步工作的想法.每个人都可以帮助我实现所需的输出

I think imnt not clear about idea of working async.each..Can someone help me to achieve the desired output

推荐答案

async.each(ListA, function (itemA, callback) { //loop through array
    //process itemA
  async.each(itemA.Children, function (itemAChild, callback1) { //loop through array
    //process itemAChild
    callback1();
    }, function(err) {
      console.log("InnerLoopFinished");
      callback();
    });
  }, function(err) {
    console.log("OuterLoopFinished");
    console.log('Process Finished');
});

这篇关于nodejs Async.each嵌套循环混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:42