【发布时间】:2021-11-13 11:42:39
【问题描述】:
考虑下面的代码:
async function why(){
try {
function noWait(callback) {
callback();
}
async function waitForTimer(callback) {
var timer = setInterval(()=>{
clearInterval(timer);
callback()
}, 10);
}
waitForTimer(()=>{
throw "this is error??"
});
noWait(()=>{
throw"this is error!!"
});
} catch (err) {
console.log(err);
}
}
why()
如何在 setInterval 函数中抛出错误并在 catch 块中捕获?
noWait() 回调中抛出的错误有效,但 waitForTimer() 回调中没有,为什么会这样?
【问题讨论】:
标签: javascript promise try-catch setinterval throw