javascript - 使用 async/await 时如何在嵌套的 Promise 中捕获错误
问题描述
async function thisThrows() {
return new Promise((resove, reject) => {
throw new Error("Thrown from thisThrows()");
});
}
async function thatThrows() {
return new Promise(async(resolve, reject) => {
try {
await thisThrows();
} catch (err) {
throw err;
}
console.log('reached here');
resolve(true);
});
}
async function run() {
try {
await thatThrows();
} catch (e) {
console.error('catch error', e);
} finally {
console.log('We do cleanup here');
}
}
run();
我的情况是我不想要语句console.log('reached here'); 解决(真);如果先前的语句抛出错误,则应执行。我尝试了几种组合,但没有运气。run 方法应该捕获错误。在我上面的代码中运行方法catch方法没有执行
解决方案
推荐阅读
- javascript - 画布上的高质量媒体记录器 30 fps at 1080p
- android - Android Landscape ConstraintLayout 视图无法正确显示
- jestjs - 在 StencilJS 测试中使用 jest-dom 会引发错误
- javascript - 为什么我的响应式网站无法在移动浏览器上正确显示?
- shell - 如果我的用户名提示不正确,我想打破它
- r - 如何使用来自其他两个数据框的信息构建数据框
- java - ArrayDeque 操作
- php - 使用 PHPMailer 对 gmail 进行身份验证
- c# - .NET Core 3.1 EntityFramework Core DatabaseModel 没有给出参数
- python - 使用 Keras 的模型 FIT 仅在 CPU 上运行良好,在 GPU 损失 NaN 上运行良好