Skip to content

Commit

Permalink
Merge pull request #1 from iaminsharingcode/master
Browse files Browse the repository at this point in the history
Fix can change status & value when has been fulfilled / rejected
  • Loading branch information
yanguango authored Apr 14, 2020
2 parents 4b88e17 + ee53366 commit 2a0aa80
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 2a0aa80

Please sign in to comment.