[英]How to ensure an error from an async function is captured without rethrowing the error or using Promise.reject?
Please, look at the following code. 请看下面的代码。 Line 5:
return ex
. 第5行:
return ex
。
When I call myFunc
, I expect to see 'err'
in the console, but I see 'yes'
which means that outside of myFunc
the error is not being captured. 当我调用
myFunc
,我希望在控制台中看到'err'
,但是我看到'yes'
,这意味着在myFunc
之外未捕获该错误。 The error is not bubbling up. 该错误没有冒泡。
Which makes sense because I'm not rethrowing the error doing: throw ex
or using Promise.reject(ex)
. 这是有道理的,因为我没有抛出错误:
throw ex
或使用Promise.reject(ex)
。
My Question is: How to ensure the error is captured outside of my function without using the two methods mentioned above? 我的问题是:如何确保不使用上述两种方法就将错误捕获到我的函数之外? Is there a way?
有办法吗?
async function myFunc() {
try {
throw new Error();
} catch (ex) {
return ex;
}
}
myFunc().then(() => console.log('yes')).catch(() => console.log('err'))
// 'yes'
The best way is to do this: 最好的方法是这样做:
async function myFunc() {
try {
throw new Error();
} catch (ex) {
throw ex;
}
}
I had an attempt at answering the question but ended up rewriting my whole answer. 我试图回答这个问题,但最终重写了我的整个答案。 At one point, I deleted my answer.
有一次,我删除了答案。
As you already stated in the question, throw ex
or Promise.reject(ex)
are the only ways. 正如您在问题中已经说过的那样,
throw ex
或Promise.reject(ex)
是唯一的方法。
When I use async function
in my application, I usually do so as a means to migrate away from Promise
chaining and dropping then
and catch
. 当我在应用程序中使用
async function
时,通常将其作为从Promise
链接迁移并删除then
catch
。 It's really odd that you mix async function
but call it with Promise
, then
and catch
syntax. 混合使用
async function
, then
用Promise
调用它, then
catch
语法,这确实很奇怪。
Actually, I think it's dangerous to mix it as indicated here: JavaScript Promises - reject vs. throw 实际上,我认为按如下所示进行混合是很危险的: JavaScript Promises-拒绝与抛出
If you indulge me, I want to rewrite your caller using async function
and await
: 如果您沉迷于我,我想使用
async function
重写您的呼叫者并await
:
async function myFunc() { throw new Error("Some Error!"); } ( async function () { try { await myFunc(); console.log("yes"); } catch (err) { console.log("err: ", err.message); } } )();
This syntax is similar to its synchronous counterpart which infers we ought to use throw
: 此语法类似于它的同步对应项,后者推断我们应该使用
throw
:
function myFunc() { throw new Error("Some Error!"); } ( function () { try { myFunc(); console.log("yes"); } catch (err) { console.log("err: ", err.message); } } )();
When using async functions, you can use the normal javascript try / catch
to handle errors. 使用异步功能时,您可以使用普通的javascript
try / catch
处理错误。
If you throw within an async function, the promise it returned will be rejected. 如果在异步函数中抛出,则它返回的promise将被拒绝。 If you return from an async function (like you did in your
catch
block), the promise will be resolved with the returned value (in your case the exception) 如果您从异步函数返回(如您在
catch
块中所做的那样),则将使用返回的值(在您的情况下为例外)来解析promise
Here two examples how you can handle an async function that might throw: 这是两个示例,说明如何处理可能引发的异步函数:
// async function that might throw async function funcThatThrows() { throw new Error("IT THROWS!"); } // Exception Handling in other async functions async function example1() { try { let result = await funcThatThrows(); /* ... */ } catch(e) { console.log("example1:", e.message); } } // Exception handling in non-async functions function example2() { funcThatThrows() .then(result => { /* ... */ }) .catch(err => console.log("example2:", err.message)); } example1(); example2();
See Javascript Async/Await Error Handling for a detailed tutorial on async error handling :) 有关异步错误处理的详细教程,请参见Javascript异步/等待错误处理:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.