Custom Promise.all
By-Shadab Ali
Learn how to implement a custom version of Promise.all in JavaScript, including step-by-step explanations and code examples.
Introduction to Promises
A promise is an object that represents the eventual completion (or failure) of an asynchronous operation, and provides a way to register callbacks to be executed when the operation completes.
Understanding Promise.all
The Promise.all method takes an array of promises and returns a single promise that resolves when all of the promises in the array have resolved, or rejects when any promise in the array rejects.
Implementing Custom Promise.all
Explanation
- Input Validation: The function checks if the input is an array. If not, it rejects with a TypeError.
- resolvedCount: Tracks how many promises have resolved.
- results: Stores the resolved values of the promises.
- promisesCount: The total number of promises.
- Edge Case for Empty Array: If the array is empty, it resolves immediately with an empty array.
Iterating Over Promises:
- The function iterates over each promise in the input array.
- Ensures that even non-promise values are treated as resolved promises.
- Handles the resolution of each promise. Increments resolvedCount. Stores the resolved value in the results array at the correct index.
If all promises have resolved
(resolvedCount === promisesCount)
, it resolves the main promise with the results array.
- Handles any rejection and immediately rejects the main promise with the encountered error.