[英]async.waterfall top level exception handler not working properly with async
我試圖制作一個頂級異常處理程序來捕獲來自 async.waterfall 的錯誤,但是在使用異步之前一切正常,請幫助我。
async function wrap(_: Function, fn: Function) {
let result: any = null;
try {
result = await fn(); // crashes when await is used
} catch (err) {
_(err);
}
_(null, result);
}
export default function (req: express.Request, res: express.Response, next: Function) {
try {
waterfall([
(next: Function) => {
wrap(next, async () => {
await delay(10000);
return true;
});
},
(a: boolean, next: Function) => {
wrap(next, () => {
if (a)
throw new Error('Error', 500); // should get catched by main try catch
});
}
], (err: any) => {
if (err)
throw err;
});
} catch (err: any) {
console.log(err);
next(err);
}
}
主要的 try catch 導致請求停止整個服務器。 刪除它並使用“下一步”代替解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.