Parallel Execution of Promises for Individual Results Retrieval Problem
Description
LeetCode Problem 2795.
Given an array functions, return a promise promise. functions is an array of functions that return promises fnPromise. Each fnPromise can be resolved or rejected.
If fnPromise is resolved:
obj = { status: "fulfilled", value: *resolved value*}
If fnPromise is rejected:
obj = { status: "rejected", reason: *reason of rejection (catched error message)*}
The promise should resolve with an array of these objects obj. Each obj in the array should correspond to the promises in the original array function, maintaining the same order.
Try to implement it without using the built-in method Promise.allSettled().
Example 1:
Input: functions = [
() => new Promise(resolve => setTimeout(() => resolve(15), 100))
]
Output: {"t":100,"values":[{"status":"fulfilled","value":15}]}
Explanation:
const time = performance.now()
const promise = promiseAllSettled(functions);
promise.then(res => {
const out = {t: Math.floor(performance.now() - time), values: res}
console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]}
})
The returned promise resolves within 100 milliseconds. Since promise from the array functions is fulfilled, the resolved value of the returned promise is set to [{"status":"fulfilled","value":15}].
Example 2:
Input: functions = [
() => new Promise(resolve => setTimeout(() => resolve(20), 100)),
() => new Promise(resolve => setTimeout(() => resolve(15), 100))
]
Output:
{
"t":100,
"values": [
{"status":"fulfilled","value":20},
{"status":"fulfilled","value":15}
]
}
Explanation: The returned promise resolves within 100 milliseconds, because the resolution time is determined by the promise that takes the longest time to fulfill. Since promises from the array functions are fulfilled, the resolved value of the returned promise is set to [{"status":"fulfilled","value":20},{"status":"fulfilled","value":15}].
Example 3:
Input: functions = [
() => new Promise(resolve => setTimeout(() => resolve(30), 200)),
() => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))
]
Output:
{
"t":200,
"values": [
{"status":"fulfilled","value":30},
{"status":"rejected","reason":"Error"}
]
}
Explanation: The returned promise resolves within 200 milliseconds, as its resolution time is determined by the promise that takes the longest time to fulfill. Since one promise from the array function is fulfilled and another is rejected, the resolved value of the returned promise is set to an array containing objects in the following order: [{"status":"fulfilled","value":30}, {"status":"rejected","reason":"Error"}]. Each object in the array corresponds to the promises in the original array function, maintaining the same order.
Constraints:
1 <= functions.length <= 10
Solution
Approach: Start all, store results by index, count completions
This is Promise.allSettled written by hand.
- Call every function right away so the promises run in parallel.
- Attach both handlers to each promise with
.then(onFulfilled, onRejected). Each one writes{ status, value }or{ status, reason }intoresults[i]. Writing by index (notpush) keeps the original order even though promises finish in any order. - Count completions; when the count reaches
functions.length, resolve the outer promise with the array.
The outer promise never rejects, because a rejection is turned into a normal result object. The total time is that of the slowest promise.
Complexity: Time O(n) bookkeeping plus the slowest promise, space O(n).
/**
* @param {Array<Function>} functions
* @return {Promise}
*/
var promiseAllSettled = function (functions) {
return new Promise(resolve => {
const results = new Array(functions.length);
let done = 0;
const finish = (i, result) => {
results[i] = result;
if (++done === functions.length) resolve(results);
};
functions.forEach((fn, i) => {
fn().then(
value => finish(i, { status: 'fulfilled', value }),
reason => finish(i, { status: 'rejected', reason }),
);
});
});
};