【问题标题】:async.js - right method for probingasync.js - 正确的探测方法
【发布时间】:2013-10-08 15:30:17
【问题描述】:

我对选择async.js 方法来探测多个替代方案并在第一个成功时停止。

例如:

async.probeSeries([
    function (callback) {
        // try something and call callback
        // without arguments - probing fails
        callback();
    },
    function (callback) {
        // try something and call callback
        // with arguments - probing successful
        callback(null, ok);
    },
    function (callback) {
        // will be not executed, because
        // the second one is successful
        callback();
    }
], function (err, result) {
    // handle the result returned by the second probe
});

我认为使用series 并以错误方式返回结果可能是一种解决方法,但有更好的方法吗?

【问题讨论】:

  • 我认为async.series 解决方案会很好,但会导致代码稍微难以阅读。将您的替代函数放入数组并使用async.doWhilst() 怎么样?
  • 感谢您对函数数组的提示 - 我可能将所有函数放入数组中,然后调用检测。

标签: javascript asynchronous async.js


【解决方案1】:

也许您正在寻找detectSeries?它的工作方式与您的示例略有不同,因为它使用相同的函数检查数组中的值,然后使用这些值之一进行回调,但也许您可以将其应用于您的问题。

顺便说一句,这看起来像是 Q 这样的 Promise 库的完美用例,您可以在其中编写

probe1().fail(probe2).fail(probe3).done(resulthandler, errhandler);

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 2017-06-06
    • 2011-08-09
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多