admin管理员组文章数量:1194384
I have a loop in node.js
for (var i in files){
var all = fs.readdirsync("./0");
async.eachSeries(all, function(item){
check(item);
}
}
The check(item)
has a callback to another function.
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)?
I have a loop in node.js
for (var i in files){
var all = fs.readdirsync("./0");
async.eachSeries(all, function(item){
check(item);
}
}
The check(item)
has a callback to another function.
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)?
Share Improve this question edited Feb 21, 2017 at 14:27 Neil 25.8k16 gold badges67 silver badges91 bronze badges asked May 26, 2014 at 6:33 Or SmithOr Smith 3,59613 gold badges47 silver badges70 bronze badges2 Answers
Reset to default 11Assuming 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
});
Outer loop needs to be async also. One of the the methods is to use 2 eachSeries loops or outer loop can be in parallel (each) if the files don't have to be processed in series:
var async = require('async');
async.eachSeries(files, function(file, outCb)
{
var all = fs.readFileSync(file);
async.eachSeries(all, function(item, inCb)
{
check(item);
inCb(null); // inner callback
},
function(err)
{
outCb(null); // outer callback
});
},
function(err)
{
console.log('all done!!!');
});
本文标签: javascriptasynceachSeries in nodejsStack Overflow
版权声明:本文标题:javascript - async.eachSeries in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738441793a2086996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论