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
Add a ment  | 

1 Answer 1

Reset to default 6

You 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