Skip to content

Instantly share code, notes, and snippets.

@the-fejw
Last active May 7, 2022 06:38
Show Gist options
  • Save the-fejw/6839754e8b43f2d74659c914eb03b19b to your computer and use it in GitHub Desktop.
Save the-fejw/6839754e8b43f2d74659c914eb03b19b to your computer and use it in GitHub Desktop.
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