Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
 - do not change status & value when fulfilled / rejected

Refer:
 1. https://promisesaplus.com/#point-14
 2. https://promisesaplus.com/#point-17
  • Loading branch information
iaminsharingcode committed Jan 12, 2020
1 parent 4b88e17 commit ee53366
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dist/src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ class Promise {
executor(this._resolve.bind(this), this._reject.bind(this));
}

_hasResolved() {
return this._isFulfilled() || this._isRejected();
}

_resolve(x) {
if (this._hasResolved()) return;

if (x === this) {
throw new TypeError("Resolving object can not be the same object");
} else if (x instanceof Promise) {
Expand Down Expand Up @@ -58,12 +64,16 @@ class Promise {
}

_fulfill(result) {
if (this._hasResolved()) return;

this._state = State.Fulfilled;
this._value = result;
this._handlers.forEach(handler => this._callHandler(handler));
}

_reject(error) {
if (this._hasResolved()) return;

this._state = State.Rejected;
this._value = error;
this._handlers.forEach(handler => this._callHandler(handler));
Expand Down
10 changes: 10 additions & 0 deletions src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ export class Promise {
executor(this._resolve.bind(this), this._reject.bind(this));
}

_hasResolved() {
return this._isFulfilled() || this._isRejected()
}

_resolve(x) {
if (this._hasResolved()) return

if (x === this) {
throw new TypeError("Resolving object can not be the same object");
} else if (x instanceof Promise) {
Expand Down Expand Up @@ -57,12 +63,16 @@ export class Promise {
}

_fulfill(result) {
if (this._hasResolved()) return

this._state = State.Fulfilled;
this._value = result;
this._handlers.forEach(handler => this._callHandler(handler));
}

_reject(error) {
if (this._hasResolved()) return

this._state = State.Rejected;
this._value = error;
this._handlers.forEach(handler => this._callHandler(handler));
Expand Down

0 comments on commit ee53366

Please sign in to comment.