Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Async.each does not stop when an iterator returns an error #288

Closed
rhythmicdevil opened this Issue Apr 8, 2013 · 2 comments

Comments

Projects
None yet
2 participants

Why does this loop not stop iterating when the value reaches 666?

Async = require('async');
var arr = [];


for(var x = 0; x < 1000; x++){
    arr.push(x);
}

Async.each(arr, 
    function(i, cb){

        console.log(i);
        if(i >= 666){
            cb('EVIL');
        } else {
            cb(null);
        }

    }, function(err){

        if(err){
            console.log('Async is ' + err);
        } else {
            console.log('Async loves you');
        }

    }
);
Contributor

brianmaissy commented Apr 8, 2013

Because it's not iterating over a loop, it's invoking the iterator function once for each element of the Array. The iterator is invoked on all elements of the Array asynchronously. If one calls back with an error, the final callback is called, but that doesn't stop (and can't stop) the other iterators from working.

If you want something which will iterate through the array sequentially, stopping if it gets an error, use eachSeries.

Thanks for info Brian.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment