【发布时间】: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