Loading problem…
Implement a Promise constructor from scratch that follows the Promise/A+ specification. This is the foundation for all Promise methods.
Create a MyPromise class that behaves like the native Promise constructor, supporting:
then method for chainingcatch method for error handlingfinally method for cleanup(resolve, reject) => {}pending, fulfilled, rejectedonFulfilled and onRejected callbackscatch and finally methodsqueueMicrotask or setTimeout as fallback// Basic promise
const p1 = new MyPromise((resolve, reject) => {
setTimeout(() => resolve('success'), 100);
});
p1.then(value => console.log(value)); // 'success'
// Error handling
const p2 = new MyPromise((resolve, reject) => {
reject('error');
});
p2.catch(err => console.log(err)); // 'error'
// Chaining
new MyPromise(resolve => resolve(1))
.then(x => x + 1)
.then(x => x * 2)
.then(console.log); // 4