作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用基于 async.series
的设计时,我必须在功能 block 中使用循环,如下所示:
var someFile = filePath;
async.series([
function(cb) {
_.forEach (
someDataList,
function (item) {
//call that executes out of sync
writeToFile(someFile,item)
//how to provide callback here
cb(); //<- is this correct - won't this take control out of this block in the very first loop iteration?
}
);
}
],
//callback function
function (err) {...}
);
最佳答案
您可以改用 forEachSeries。
var someFile = filePath;
async.forEachSeries(someDataList, function(item, cb) {
writeToFile(someFile, item)
cb();
},
//callback function
function () {...}
);
评论后更新:
不幸的是,无法在 forEachSeries 中获取迭代对象的索引。如果你想要 iteratee 的索引,你可以使用 eachOfSeries
var someFile = filePath;
async.eachOfSeries(someDataList, function(value, key, callback) {
//Do something with key
writeToFile(someFile, item)
callback();
}, //callback function
function () {...}
);
关于javascript - nodejs 异步 : How to use a loop containing an asynchronous call within an "async.series" function block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48166469/
我是一名优秀的程序员,十分优秀!