Implement myRace, myAny, myAll, and myAllSettled on Promise
MDN’s Promise reference documents each of the static Promise methods. Extend the Promise object in JavaScript by implementing custom methods that mimic the functionality of existing Promise methods. Specifically, you are required to create the following methods:
Promise.myRace: This method should mimic the behavior of Promise.race, returning a promise that resolves or rejects as soon as one of the promises in the input array does, reflecting that promise’s outcome.Promise.myAny: Similar to Promise.any, this method should return a promise that resolves as soon as any one of the input promises resolves, with the value of the resolved promise. If all input promises are rejected, it should reject with an aggregate error.Promise.myAll: This method should emulate Promise.all, returning a promise that resolves when all of the input promises have resolved, with an array of the results, or rejects as soon as one of the input promises rejects.Promise.myAllSettled: Inspired by Promise.allSettled, this method should return a promise that resolves after all input promises have settled (resolved or rejected), with an array of objects that each describe the outcome of each promise.For example, using Promise.myRace, you can race multiple promises against each other and act upon the first to settle, whether it resolves or rejects:
Promise.myRace([Promise.resolve('First'), Promise.reject('Second'), Promise.resolve('Third')])
.then(value => console.log(value))
.catch(error => console.error(error));
// Expected output: "First"
Implementing these methods will give you deeper insights into the workings of promises in JavaScript, enhancing your understanding of asynchronous programming patterns.