admin管理员组文章数量:1356045
Can somebody explain me how I can to do callback only after all iteration was finished? I used async.each function for that:
async.each(products, function (product, callback) {
fs.appendFile('ParseLog.txt', "PRODUCT name: " + product.name, function (err) {
console.log("iterate");
callback();
});
}, function (err) {
console.log("ALL FINISH");
});
So my input looks like:
ALL FINISH
iterate
iterate
iterate
...
but I expect that "ALL FINISH" message will prints AFTER all iteration.
EDIT 1:
Sorry but it seems trouble was in if(i > 10) return callback({ data: 'hi'}); // stop
at start of each loop.
I just want to exit after 11 iteration, but it stranges to me why its do callback at first.?
async.each(products, function (product, callback) {
var i = products.indexOf(product);
if(i > 10) return callback({ data: 'hi'}); // stop
fs.appendFile('ParseLog.txt', "PRODUCT name: " + product.name, function (err) {
console.log("iterate");
callback();
});
..
Can somebody explain me how I can to do callback only after all iteration was finished? I used async.each function for that:
async.each(products, function (product, callback) {
fs.appendFile('ParseLog.txt', "PRODUCT name: " + product.name, function (err) {
console.log("iterate");
callback();
});
}, function (err) {
console.log("ALL FINISH");
});
So my input looks like:
ALL FINISH
iterate
iterate
iterate
...
but I expect that "ALL FINISH" message will prints AFTER all iteration.
EDIT 1:
Sorry but it seems trouble was in if(i > 10) return callback({ data: 'hi'}); // stop
at start of each loop.
I just want to exit after 11 iteration, but it stranges to me why its do callback at first.?
async.each(products, function (product, callback) {
var i = products.indexOf(product);
if(i > 10) return callback({ data: 'hi'}); // stop
fs.appendFile('ParseLog.txt', "PRODUCT name: " + product.name, function (err) {
console.log("iterate");
callback();
});
..
Share
Improve this question
edited Jul 14, 2015 at 18:42
MeetJoeBlack
asked Jul 14, 2015 at 18:12
MeetJoeBlackMeetJoeBlack
2,92410 gold badges42 silver badges70 bronze badges
3
- Yes you can use promises to do that – KlwntSingh Commented Jul 14, 2015 at 18:23
- 2 Is the code that you're using the exact code that you're posting here? I don't see how it can cause what you're seeing. – robertklep Commented Jul 14, 2015 at 18:31
- sorry, my blame see my updates – MeetJoeBlack Commented Jul 14, 2015 at 18:43
1 Answer
Reset to default 6You should limit the number of products to process before you pass it into async.each
:
async.each(products.slice(0, 11), function (product, callback) {
fs.appendFile('ParseLog.txt', "PRODUCT name: " + product.name, function (err) {
console.log("iterate");
callback(err); // make sure to pass `err`!
});
}, function(err) {
if (err) console.log('ERROR', err);
console.log("ALL FINISH");
});
Also, when you call a continuation callback, the first argument is "reserved" to signal that an error occurred. In your example, you're using it to pass an object ({ data: 'hi' }
) which will make async
think that an error happened (unless that was your intention?). The proper idiom is this:
callback(null, { data: 'hi' })
本文标签: javascriptnodejs asynceach() callback was called before all iteration was finishedStack Overflow
版权声明:本文标题:javascript - node.js async.each() callback was called before all iteration was finished - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744054182a2582970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论