【问题标题】:Is it possible to finish async.js loop early?是否可以提前完成 async.js 循环?
【发布时间】:2016-11-22 02:24:15
【问题描述】:

我正在寻找一种在调用所有回调之前尽早完成 async.js 循环的方法。是否有任何内置功能可以做到这一点?如果没有,有什么办法可以实现所需的功能?

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

    if (condition == true) {

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

    }

    cb()

}, function(err){


})

【问题讨论】:

标签: javascript async.js


【解决方案1】:

是的,有可能,你应该像这样将错误参数传递给你的回调函数:

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

如果条件为真,循环将停止。

async.forEachOf documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    相关资源
    最近更新 更多