Skip to content

Commit

Permalink
Remove try/catch avoidance hacks as they are no longer needed (as per @…
Browse files Browse the repository at this point in the history
…bmeurer’s guidance)
  • Loading branch information
stefanpenner committed Feb 16, 2019
1 parent 95e8fbf commit e33ab9d
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 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 @@ -38,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 @@ -78,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 Down Expand Up @@ -172,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

0 comments on commit e33ab9d

Please sign in to comment.