0

I am looking for a way to finish async.js loop early before all the callbacks get called. Is there any built-in function to do so? If not, is there any way around to achieve the desired functionality?

async.forEachOf(children, function(child, i, cb){

    if (condition == true) {

        // finish early - call cb() for all iterations

    }

    cb()

}, function(err){


})
sawa
  • 1,930
  • 3
  • 24
  • 33

1 Answers1

0

Yes it is possible, you should pass a error parameter into your callback function like this:

async.forEachOf(children, function(child, i, cb){
    ....
    cb(condition)
}, function(err){
   // Error handling
})

If condition is True the loop will stop.

async.forEachOf documentation

Isidro Martínez
  • 816
  • 7
  • 15