Implement Custom Promise
A Promise is a fundamental concept in JavaScript for handling asynchronous operations. Understanding how to implement a custom Promise will deepen your understanding of asynchronous programming and help you in interviews.
Problem Statement
Implement a custom Promise class with the following methods:
constructor(executor)- Initialize the promise with an executor functionthen(onFulfilled, onRejected)- Handle successful and failed statescatch(onRejected)- Handle only rejected statesfinally(onFinally)- Execute regardless of statestatic resolve(value)- Create a resolved promisestatic reject(reason)- Create a rejected promisestatic all(promises)- Wait for all promises to resolvestatic race(promises)- Return the first settled promise