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) ());
评论
发表评论