@@ -20,27 +20,32 @@ export class AbortError extends Error {
2020/**
2121TODO: Remove AbortError and just throw DOMException when targeting Node 18.
2222*/
23- const getDOMException = errorMessage => globalThis . DOMException === undefined ?
24- new AbortError ( errorMessage ) :
25- new DOMException ( errorMessage ) ;
23+ const getDOMException = errorMessage => globalThis . DOMException === undefined
24+ ? new AbortError ( errorMessage )
25+ : new DOMException ( errorMessage ) ;
2626
2727/**
2828TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.
2929*/
3030const getAbortedReason = signal => {
31- const reason = signal . reason === undefined ?
32- getDOMException ( 'This operation was aborted.' ) :
33- signal . reason ;
31+ const reason = signal . reason === undefined
32+ ? getDOMException ( 'This operation was aborted.' )
33+ : signal . reason ;
3434
3535 return reason instanceof Error ? reason : getDOMException ( reason ) ;
3636} ;
3737
3838export default function pTimeout ( promise , options ) {
39+ const {
40+ milliseconds,
41+ fallback,
42+ message,
43+ customTimers = { setTimeout, clearTimeout} ,
44+ } = options ;
45+
3946 let timer ;
4047
4148 const cancelablePromise = new Promise ( ( resolve , reject ) => {
42- const { milliseconds, fallback, message} = options ;
43-
4449 if ( typeof milliseconds !== 'number' || Math . sign ( milliseconds ) !== 1 ) {
4550 throw new TypeError ( `Expected \`milliseconds\` to be a positive number, got \`${ milliseconds } \`` ) ;
4651 }
@@ -50,11 +55,6 @@ export default function pTimeout(promise, options) {
5055 return ;
5156 }
5257
53- options = {
54- customTimers : { setTimeout, clearTimeout} ,
55- ...options
56- } ;
57-
5858 if ( options . signal ) {
5959 const { signal} = options ;
6060 if ( signal . aborted ) {
@@ -66,7 +66,7 @@ export default function pTimeout(promise, options) {
6666 } ) ;
6767 }
6868
69- timer = options . customTimers . setTimeout . call ( undefined , ( ) => {
69+ timer = customTimers . setTimeout . call ( undefined , ( ) => {
7070 if ( fallback ) {
7171 try {
7272 resolve ( fallback ( ) ) ;
@@ -93,13 +93,13 @@ export default function pTimeout(promise, options) {
9393 } catch ( error ) {
9494 reject ( error ) ;
9595 } finally {
96- options . customTimers . clearTimeout . call ( undefined , timer ) ;
96+ customTimers . clearTimeout . call ( undefined , timer ) ;
9797 }
9898 } ) ( ) ;
9999 } ) ;
100100
101101 cancelablePromise . clear = ( ) => {
102- clearTimeout ( timer ) ;
102+ customTimers . clearTimeout . call ( undefined , timer ) ;
103103 timer = undefined ;
104104 } ;
105105
0 commit comments