const nativeExceptions = [ EvalError , RangeError , ReferenceError , SyntaxError , TypeError , URIError ] . filter ( ( except ) => typeof except === 'function' ) ; function throwNative ( error ) { for ( const Exception of nativeExceptions ) { if ( error instanceof Exception ) { throw error ; } } } function emptyFn ( ) { } type SafePromiseError = [ Error , null ] ; type SafePromiseResolve < T > = [ null , T ] ; type SafePromise < T > = Promise < SafePromiseError | SafePromiseResolve < T > > ; export function safeAwait < T > ( promise , finallyFunc = emptyFn ) { return promise . then ( ( data ) => { if ( data instanceof Error ) { throwNative ( data ) ; return [ data , null ] as SafePromiseError ; } return [ null , data ] as SafePromiseResolve < T > ; } ) . catch ( ( error ) => { throwNative ( error ) ; return [ error , null ] as SafePromiseError ; } ) . finally ( finallyFunc ) ; }