Skip to content

Commit

Permalink
Merge pull request #317 from bekzod/cleanup-error-holder
Browse files Browse the repository at this point in the history
use one error holder
  • Loading branch information
stefanpenner committed Jan 12, 2018
2 parents 97478eb + cb16028 commit 4abbe5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
17 changes: 6 additions & 11 deletions lib/es6-promise/-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const PENDING = void 0;
const FULFILLED = 1;
const REJECTED = 2;

const GET_THEN_ERROR = new ErrorObject();
const TRY_CATCH_ERROR = { error: null };

function selfFulfillment() {
return new TypeError("You cannot resolve a promise with itself");
Expand All @@ -32,8 +32,8 @@ function getThen(promise) {
try {
return promise.then;
} catch(error) {
GET_THEN_ERROR.error = error;
return GET_THEN_ERROR;
TRY_CATCH_ERROR.error = error;
return TRY_CATCH_ERROR;
}
}

Expand Down Expand Up @@ -87,9 +87,9 @@ function handleMaybeThenable(promise, maybeThenable, then) {
maybeThenable.constructor.resolve === originalResolve) {
handleOwnThenable(promise, maybeThenable);
} else {
if (then === GET_THEN_ERROR) {
reject(promise, GET_THEN_ERROR.error);
GET_THEN_ERROR.error = null;
if (then === TRY_CATCH_ERROR) {
reject(promise, TRY_CATCH_ERROR.error);
TRY_CATCH_ERROR.error = null;
} else if (then === undefined) {
fulfill(promise, maybeThenable);
} else if (isFunction(then)) {
Expand Down Expand Up @@ -174,11 +174,6 @@ function publish(promise) {
promise._subscribers.length = 0;
}

function ErrorObject() {
this.error = null;
}

const TRY_CATCH_ERROR = new ErrorObject();

function tryCatch(callback, detail) {
try {
Expand Down
10 changes: 6 additions & 4 deletions lib/es6-promise/enumerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
isArray,
isMaybeThenable
} from './utils';

import {
noop,
reject,
Expand Down Expand Up @@ -109,7 +108,10 @@ export default class Enumerator {
_willSettleAt(promise, i) {
let enumerator = this;

subscribe(promise, undefined, value => enumerator._settledAt(FULFILLED, i, value),
reason => enumerator._settledAt(REJECTED, i, reason));
subscribe(
promise, undefined,
value => enumerator._settledAt(FULFILLED, i, value),
reason => enumerator._settledAt(REJECTED, i, reason)
);
}
}
};
20 changes: 10 additions & 10 deletions lib/es6-promise/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ export default function polyfill() {
let local;

if (typeof global !== 'undefined') {
local = global;
local = global;
} else if (typeof self !== 'undefined') {
local = self;
local = self;
} else {
try {
local = Function('return this')();
} catch (e) {
throw new Error('polyfill failed because global object is unavailable in this environment');
}
try {
local = Function('return this')();
} catch (e) {
throw new Error('polyfill failed because global object is unavailable in this environment');
}
}

let P = local.Promise;

if (P) {
var promiseToString = null;
try {
promiseToString = Object.prototype.toString.call(P.resolve());
promiseToString = Object.prototype.toString.call(P.resolve());
} catch(e) {
// silently ignored
// silently ignored
}

if (promiseToString === '[object Promise]' && !P.cast){
return;
return;
}
}

Expand Down
2 changes: 0 additions & 2 deletions lib/es6-promise/promise.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
isFunction
} from './utils';

import {
noop,
nextId,
PROMISE_ID,
initializePromise
} from './-internal';

import {
asap,
setAsap,
Expand Down

0 comments on commit 4abbe5e

Please sign in to comment.