How to deal with asynchronous exception gracefully in js

How to deal with asynchronous exception gracefully in js

How to deal with asynchronous exception gracefully in js
setTimeout(() => {
    throw new Error("Whoops!");
}, 1000);
For example, in the above scenario, the outer try-catch and other methods must not be handled, and window.onerror is not considered.
In addition to using Promise.reject to rewrite the above code as
new Promise((resolve, reject) => {
    setTimeout(() => {
        reject("Whoops!");
    }, 1000);
}).catch(/* errorHandle */)
Or is there any alternative to try-catch inside setTimout?

async / await?
setTimeout(() => {
    try {
        throw new Error("Whoops!");
    } catch {
        // handle error
    }
}, 1000);
like this?
async / await + try - catch
There is also an inelegant
window.addEventListener('unhandledrejection', function (err) ());

评论

此博客中的热门博文

将博客部署到星际文件系统(IPFS)

高中地理必修一知识点总结

一场CF的台前幕后(下)