[英]async/await catch rejected Promises
I'm just getting started with async/await
and running into a problem. 我刚刚开始使用
async/await
并遇到问题。
I can do as expected: 我可以按预期做到:
async function x() {
let y = await Promise.resolve(42);
return y;
}
But when I reject a Promise
: 但是当我拒绝
Promise
:
async function bad() {
try {
await Promise.reject('bad');
} catch(err) {
err; //AssertionError: TypeError: (0 , _errorHandler2.default) is not a function
}
}
How do I catch rejected Promises
with async/await
? 如何使用
async/await
捕获rejected Promises
?
What bad;
什么
bad;
alone is supposed to do? 应该独自做什么? The error is caught as expected, you just don't do anything with it:
错误按预期捕获,您只是不对它执行任何操作:
async function bad() {
try {
await Promise.reject('bad');
} catch(err) {
console.log(err);
}
}
bad();
This outputs bad
as expected. 这按预期输出
bad
。 Code here . 代码在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.