Skip to content

Commit

Permalink
Merge pull request #345 from stefanpenner/remove-try-catch-hacks
Browse files Browse the repository at this point in the history
Remove try/catch avoidance hacks
  • Loading branch information
stefanpenner committed Jun 6, 2019
2 parents 9869a4b + e33ab9d commit 21e755a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
59 changes: 18 additions & 41 deletions lib/es6-promise/-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const PENDING = void 0;
const FULFILLED = 1;
const REJECTED = 2;

const TRY_CATCH_ERROR = { error: null };

function selfFulfillment() {
return new TypeError("You cannot resolve a promise with itself");
}
Expand All @@ -28,15 +26,6 @@ function cannotReturnOwn() {
return new TypeError('A promises callback cannot return that same promise.');
}

function getThen(promise) {
try {
return promise.then;
} catch(error) {
TRY_CATCH_ERROR.error = error;
return TRY_CATCH_ERROR;
}
}

function tryThen(then, value, fulfillmentHandler, rejectionHandler) {
try {
then.call(value, fulfillmentHandler, rejectionHandler);
Expand All @@ -47,8 +36,8 @@ function tryThen(then, value, fulfillmentHandler, rejectionHandler) {

function handleForeignThenable(promise, thenable, then) {
asap(promise => {
var sealed = false;
var error = tryThen(then, thenable, value => {
let sealed = false;
let error = tryThen(then, thenable, value => {
if (sealed) { return; }
sealed = true;
if (thenable !== value) {
Expand Down Expand Up @@ -87,10 +76,7 @@ function handleMaybeThenable(promise, maybeThenable, then) {
maybeThenable.constructor.resolve === originalResolve) {
handleOwnThenable(promise, maybeThenable);
} else {
if (then === TRY_CATCH_ERROR) {
reject(promise, TRY_CATCH_ERROR.error);
TRY_CATCH_ERROR.error = null;
} else if (then === undefined) {
if (then === undefined) {
fulfill(promise, maybeThenable);
} else if (isFunction(then)) {
handleForeignThenable(promise, maybeThenable, then);
Expand All @@ -104,7 +90,14 @@ function resolve(promise, value) {
if (promise === value) {
reject(promise, selfFulfillment());
} else if (objectOrFunction(value)) {
handleMaybeThenable(promise, value, getThen(value));
let then;
try {
then = value.then;
} catch (error) {
reject(promise, error);
return;
}
handleMaybeThenable(promise, value, then);
} else {
fulfill(promise, value);
}
Expand Down Expand Up @@ -174,46 +167,31 @@ function publish(promise) {
promise._subscribers.length = 0;
}


function tryCatch(callback, detail) {
try {
return callback(detail);
} catch(e) {
TRY_CATCH_ERROR.error = e;
return TRY_CATCH_ERROR;
}
}

function invokeCallback(settled, promise, callback, detail) {
let hasCallback = isFunction(callback),
value, error, succeeded, failed;
value, error, succeeded = true;

if (hasCallback) {
value = tryCatch(callback, detail);

if (value === TRY_CATCH_ERROR) {
failed = true;
error = value.error;
value.error = null;
} else {
succeeded = true;
try {
value = callback(detail);
} catch (e) {
succeeded = false;
error = e;
}

if (promise === value) {
reject(promise, cannotReturnOwn());
return;
}

} else {
value = detail;
succeeded = true;
}

if (promise._state !== PENDING) {
// noop
} else if (hasCallback && succeeded) {
resolve(promise, value);
} else if (failed) {
} else if (succeeded === false) {
reject(promise, error);
} else if (settled === FULFILLED) {
fulfill(promise, value);
Expand Down Expand Up @@ -249,7 +227,6 @@ function makePromise(promise) {
export {
nextId,
makePromise,
getThen,
noop,
resolve,
reject,
Expand Down
17 changes: 14 additions & 3 deletions lib/es6-promise/enumerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
FULFILLED,
REJECTED,
PENDING,
getThen,
handleMaybeThenable
} from './-internal';

Expand Down Expand Up @@ -63,7 +62,15 @@ export default class Enumerator {
let { resolve } = c;

if (resolve === originalResolve) {
let then = getThen(entry);
let then;
let error;
let didError = false;
try {
then = entry.then;
} catch (e) {
didError = true;
error = e;
}

if (then === originalThen &&
entry._state !== PENDING) {
Expand All @@ -73,7 +80,11 @@ export default class Enumerator {
this._result[i] = entry;
} else if (c === Promise) {
let promise = new c(noop);
handleMaybeThenable(promise, entry, then);
if (didError) {
reject(promise, error);
} else {
handleMaybeThenable(promise, entry, then);
}
this._willSettleAt(promise, i);
} else {
this._willSettleAt(new c(resolve => resolve(entry)), i);
Expand Down

0 comments on commit 21e755a

Please sign in to comment.