Last active
May 7, 2022 06:38
-
-
Save the-fejw/6839754e8b43f2d74659c914eb03b19b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const state = { | |
PENDING: 'pending', | |
FULFILLED: 'fulfilled', | |
REJECTED: 'rejected' | |
}; | |
class MyPromise { | |
constructor(executor) { | |
// initial state | |
this._state = state.PENDING; | |
this._value = undefined; | |
this._error = undefined; | |
// a list to save promise chains before promise is settled | |
this._thenQueue = []; | |
const resolve = this._onFulfilled.bind(this); | |
const reject = this._onRejected.bind(this); | |
executor(resolve, reject); | |
} | |
/** start: pseudo internal methods */ | |
_onFulfilled(value) {} | |
_onRejected(error) {} | |
/** end: pseudo internal methods */ | |
/** start: exposed methods */ | |
then(fulfillFn, rejectFn) {} | |
catch(rejectFn) {} | |
/** end: exposed methods */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment