【发布时间】:2016-11-17 17:48:24
【问题描述】:
我正在使用异步瀑布。当我的一个函数调用回调(错误)时,我的自定义异步回调被调用。在里面我抛出一个错误,希望它会在异步的 try 块中被捕获,但那没有发生。
try {
async.waterfall([function1, function2], myAsyncCallback);
}
catch(err) {
console.log("THIS CODE IS NEVER EXECUTED.");
}
var function1 = function() {
...
//some error occurs:
callback(new Error(errMsg), errMsg);
...
}
var function2 = function() {
...
}
function myAsyncCallback(err, result) {
console.log("This code gets executed.");
if (err) {
console.log("This code gets executed too.");
throw new Error("I want this error caught at the top around the catch around async.waterfall()");
}
}
【问题讨论】:
-
这就是代码格式的重要性:现在你声明
myAsyncCallbackinside 在你的function1中,所以这不是真正明智的代码。您能否将其减少为minimal reproducible example,人们可以将其复制粘贴到文件并在节点中运行以查看您看到的相同内容,而不是发布保证不会因为无法运行而显示您的问题的代码? -
@Mike,它们实际上并不在彼此的内部。更新了示例,因此希望可以清楚地说明。您应该能够删除“...”来测试它。
-
我添加了一个答案,但可能需要更深入地了解被调用的函数。我发布了我的工作代码,看起来很像你的例子!
标签: javascript node.js