node.js中的async.eachSeries [英] async.eachSeries in node.js

查看:119
本文介绍了node.js中的async.eachSeries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在中有一个循环node.js

for (var i in files){
    var all = fs.readdirsync("./0");
    async.eachSeries(all, function(item){
        check(item); 
   }
}

check(item)有一个回调给另一个函数。

The check(item) has a callback to another function.

正如我所看到的, async.eachSeries 不会同步执行。循环继续执行其他项,在 check()函数完成。

As I can see, the async.eachSeries doesn't execute synchronously. The loop continues to execute the other items, before the callback in the check() function is finish.

如何让循环等到迭代完成(包括回调)?

How do I make the loop wait until the iteration is finished (including the callback)?

推荐答案

假设检查接受回调,我们可以使用 mapSeries 实现这一目标。

Assuming check accepts a callback, we can use mapSeries to achieve that.

async.mapSeries(files, function(file, outerCB) {
  var all = fs.readdirsync("./0");
  async.mapSeries(all, function(item, cb){
      check(item, cb);
  }, outerCB);
}, function(err, results) {
  // This is called when everything's done
});

这篇关于node.js中的async.eachSeries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆