




Sponsored
Sponsored
This approach involves managing a counter to track resolved promises and ensuring we handle rejection immediately. We loop through each of the asynchronous functions, initiate their execution, and attach then and catch handlers. Using shared variables, we manage the resolved values and first rejection detection.
1function promiseAll(functions) {
2    return new Promise((This code creates a promise that resolves when all asynchronous functions have resolved, or rejects immediately if any promise is rejected. We maintain a results array to store resolved values and a completed counter to know when all functions have resolved. Each function is called, and on success, we store its result and increment the completed counter. Upon completion of all functions, we resolve the promise with the results.
This approach focuses on using individual promise resolution for each function and using a shared state to track the first rejection or when all have completed.
1function promiseAll(functions) {
2    const results = new Array(functions.length);
3    let counter = 0;
4
5    return new Promise((resolve, reject) => {
6        functions.forEach((func, index) => {
7            func().then(value => {
8                results[index] = value;
9                counter++;
10                if (counter === functions.length) {
11                    resolve(results);
12                }
13            }).catch(err => reject(err));
14        });
15    });
16}This solution initiates each asynchronous function, attaches promise handlers to manage results, and uses a counter to track the number of resolved promises. On the resolution of any promise, we store its result and increase the counter. If all are resolved, we resolve our overall promise with the results. Any rejection leads to an immediate promise rejection.
This approach manually handles the resolution and rejection of each promise returned by the functions. We'll iterate over the functions list, execute each one in parallel, and use counters or markers to determine when all promises have settled. Upon completion, their results will be consolidated into a single array, keeping track of the order.
Time Complexity: O(n) for n functions and promises.
Space Complexity: O(n) storing the resolved values.
1function promiseAll(functions) {
2  return new Promise((resolve, reject) => {
3    const results = [];
4    let completed = 0;
5    functions.forEach((func, index) => {
6      func().then(result => {
7        results[index] = result;
8        completed++;
9        if (completed === functions.length) {
10          resolve(results);
11        }
12      }).catch(reject);
13    });
14  });
15}Here, a counter is used to ensure all promises have resolved before resolving the overall promise. This approach maintains state using closure variables to track the number of resolved promises and collects results into predefined storage (array).
Time Complexity: O(n) because each promise executes independently.
Space Complexity: O(n) due to the results array storing each resolved value.
1function promiseAll(functions) {
2  return new Promise((resolve, reject) => {
3    const results = Array(functions.length);
4    let resolvedCount = 0;
5    functions.forEach((func, index) => {
6      func().then(result => {
7        results[index] = result;
8        resolvedCount++;
9        if (resolvedCount === functions.length) {
10          resolve(results);
11        }
12      }).catch(reject);
13    });
14  });
15}