博文

目前显示的是标签为“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 ) ( ));