try catch捕获不到异步里的异常

try catch捕获不到异步里的异常

例如:

function fn() {
    setTimeout(() => {
        throw new Error();
    }, 3000);
}
try {
    console.log(1)
    fn()
    console.log(2)
} catch(e) {
    console.log(3, e)
}

但是promise能够处理异常。

你可能感兴趣的:(前端开发,javascript,开发语言,ecmascript)