Skip to content

Commit

Permalink
Don't allocate an array if no waiting until after resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay committed Nov 19, 2015
1 parent 6d4ff82 commit 4680c50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ function Promise(fn) {
if (typeof fn !== 'function') {
throw new TypeError('not a function');
}
this._state = 0;
this._state = -1;
this._value = null;
this._deferreds = [];
this._deferreds = null;
if (fn === noop) return;
doResolve(fn, this);
}
Expand All @@ -86,6 +86,11 @@ function handle(self, deferred) {
while (self._state === 3) {
self = self._value;
}
if (self._state === -1) {
self._state = 0;
self._deferreds = [deferred];
return;
}
if (self._state === 0) {
self._deferreds.push(deferred);
return;
Expand Down Expand Up @@ -148,10 +153,12 @@ function reject(self, newValue) {
finale(self);
}
function finale(self) {
for (var i = 0; i < self._deferreds.length; i++) {
handle(self, self._deferreds[i]);
if (self._deferreds) {
for (var i = 0; i < self._deferreds.length; i++) {
handle(self, self._deferreds[i]);
}
self._deferreds = null;
}
self._deferreds = null;
}

function Handler(onFulfilled, onRejected, promise){
Expand Down
3 changes: 3 additions & 0 deletions src/synchronous.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Promise.enableSynchronous = function () {
if (this._state === 3) {
return this._value.getState();
}
if (this._state === -1) {
return 0;
}

return this._state;
};
Expand Down

0 comments on commit 4680c50

Please sign in to comment.