@@ -411,31 +411,25 @@ Worker.prototype.updateProgress = function updateProgress(val, state, n, stack)
411
411
/* ----- PROMISE MAPPING ----- */
412
412
413
413
Worker . prototype . then = function then ( onFulfilled , onRejected ) {
414
- // Wrap `this` for encapsulation and bind it to the promise handlers .
414
+ // Wrap `this` for encapsulation.
415
415
var self = this ;
416
- if ( onFulfilled ) { onFulfilled = onFulfilled . bind ( self ) ; }
417
- if ( onRejected ) { onRejected = onRejected . bind ( self ) ; }
418
-
419
- // Cast self into a Promise to avoid polyfills recursively defining `then`.
420
- var selfPromise = ( Promise . toString ( ) . indexOf ( '[native code]' ) === - 1 ) ?
421
- Worker . convert ( Object . assign ( { } , self ) , Promise . prototype ) : self ;
422
416
423
- // Update progress while queuing, calling, and resolving `then`.
424
- self . updateProgress ( null , null , 1 , [ onFulfilled ] ) ;
425
- var returnVal = Promise . prototype . then . call ( selfPromise , function then_pre ( val ) {
426
- self . updateProgress ( null , onFulfilled ) ;
427
- return val ;
428
- } ) . then ( onFulfilled , onRejected ) . then ( function then_post ( val ) {
429
- self . updateProgress ( 1 ) ;
430
- return val ;
417
+ return this . thenCore ( onFulfilled , onRejected , function then_main ( onFulfilled , onRejected ) {
418
+ // Update progress while queuing, calling, and resolving `then`.
419
+ self . updateProgress ( null , null , 1 , [ onFulfilled ] ) ;
420
+ return Promise . prototype . then . call ( this , function then_pre ( val ) {
421
+ self . updateProgress ( null , onFulfilled ) ;
422
+ return val ;
423
+ } ) . then ( onFulfilled , onRejected ) . then ( function then_post ( val ) {
424
+ self . updateProgress ( 1 ) ;
425
+ return val ;
426
+ } ) ;
431
427
} ) ;
432
-
433
- // Return the promise, after casting it into a Worker and preserving props.
434
- return Worker . convert ( returnVal , self . __proto__ ) ;
435
428
} ;
436
429
437
- Worker . prototype . thenCore = function thenCore ( onFulfilled , onRejected ) {
438
- // Core version of then, with no updates to progress.
430
+ Worker . prototype . thenCore = function thenCore ( onFulfilled , onRejected , thenBase ) {
431
+ // Handle optional thenBase parameter.
432
+ thenBase = thenBase || Promise . prototype . then ;
439
433
440
434
// Wrap `this` for encapsulation and bind it to the promise handlers.
441
435
var self = this ;
@@ -447,7 +441,7 @@ Worker.prototype.thenCore = function thenCore(onFulfilled, onRejected) {
447
441
Worker . convert ( Object . assign ( { } , self ) , Promise . prototype ) : self ;
448
442
449
443
// Return the promise, after casting it into a Worker and preserving props.
450
- var returnVal = Promise . prototype . then . call ( selfPromise , onFulfilled , onRejected ) ;
444
+ var returnVal = thenBase . call ( selfPromise , onFulfilled , onRejected ) ;
451
445
return Worker . convert ( returnVal , self . __proto__ ) ;
452
446
} ;
453
447
0 commit comments