Implement a custom MyPromise
class that reproduces the core behaviour of the native JavaScript Promise
.
(resolve, reject) => void
.then(onFulfilled?, onRejected?)
catch(onRejected?)
finally(onFinally?)
then
returns a new promise to enable chaining.then
.finally
executes on both resolve and reject.const p = new MyPromise((resolve) => {
setTimeout(() => resolve(42), 100);
});
p.then(v => v * 2)
.then(console.log); // 84 (after ~100 ms)
new MyPromise((_, reject) => reject('error'))
.catch(console.log); // "error"