Skip to content

Commit 101b6a0

Browse files
committed
Rewrite then to use thenCore internally
1 parent a7be7db commit 101b6a0

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/worker.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -411,31 +411,25 @@ Worker.prototype.updateProgress = function updateProgress(val, state, n, stack)
411411
/* ----- PROMISE MAPPING ----- */
412412

413413
Worker.prototype.then = function then(onFulfilled, onRejected) {
414-
// Wrap `this` for encapsulation and bind it to the promise handlers.
414+
// Wrap `this` for encapsulation.
415415
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;
422416

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+
});
431427
});
432-
433-
// Return the promise, after casting it into a Worker and preserving props.
434-
return Worker.convert(returnVal, self.__proto__);
435428
};
436429

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;
439433

440434
// Wrap `this` for encapsulation and bind it to the promise handlers.
441435
var self = this;
@@ -447,7 +441,7 @@ Worker.prototype.thenCore = function thenCore(onFulfilled, onRejected) {
447441
Worker.convert(Object.assign({}, self), Promise.prototype) : self;
448442

449443
// 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);
451445
return Worker.convert(returnVal, self.__proto__);
452446
};
453447

0 commit comments

Comments
 (0)