Promise Implementation
JS Functions
Hard
45 min
promises
async
event-loop
classes
Implement a class MyPromise that behaves like a native Promise for the core features. It's roughly the Promises/A+ specification plus catch, finally and two static helpers.
const p = new MyPromise((resolve) => setTimeout(() => resolve(2), 10));
p.then((n) => n * 10)
.then((n) => {
throw new Error(`got ${n}`);
})
.catch((e) => e.message) // 'got 20'
.finally(() => console.log('done'))
.then(console.log); // 'got 20'
Requirements
Constructor new MyPromise(executor)
- Throws a
TypeErrorifexecutoris not a function. - Calls
executor(resolve, reject)synchronously. If the executor throws, the promise rejects with the error, unless it was already resolved. - Only the first call to
resolveorrejecthas any effect.
Resolution (resolve(x))
- If
xis the promise itself, reject with aTypeError. - If
xis an object or function with a callablethen(a thenable, includingMyPromiseand native promises), adopt its state: callx.then(resolveFn, rejectFn)in a later microtask, with a fresh pair of first-call-wins functions. If readingthenthrows, or calling it throws before settling, reject with that error. - Otherwise fulfill with
x.
then(onFulfilled, onRejected)
- Returns a new
MyPromise. - Callbacks always run asynchronously as microtasks (use
queueMicrotask), even if the promise is already settled, and in the order they were registered. - A non-function argument is ignored, and the value or reason passes through to the returned promise.
- The returned promise is resolved with the callback's return value (with the same adoption rules), or rejected if the callback throws.
catch(onRejected) is then(undefined, onRejected).
finally(onFinally)
- Calls
onFinally()with no arguments on either outcome. - Passes the original value or reason through, after waiting for any promise
onFinallyreturns. - If
onFinallythrows or returns a rejected promise, the result rejects with that reason instead.
Statics: MyPromise.resolve(value) returns value itself if it is a MyPromise, otherwise a new MyPromise resolved with it. MyPromise.reject(reason) returns a new rejected MyPromise.
You may not use the native Promise inside MyPromise.
Follow-up
- Why must
thencallbacks always be asynchronous, even for a promise that is already settled? - How would you add
MyPromise.all, and how would you detect unhandled rejections?
Spotted a mistake or a rights concern? Report an issue