Parallel Execution of Promises for Individual Results Retrieval
Problem
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().
Examples
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
Method 1 – Manual AllSettled Simulation
Intuition
We want to run all promise-returning functions in parallel and collect their results in order, reporting both fulfilled and rejected outcomes. Since we can't use Promise.allSettled, we simulate it by wrapping each promise to always resolve with an object describing its status and value or reason.
Approach
- For each function in the input array, call it to get a promise.
- For each promise, attach
.thenand.catchhandlers to convert the result into an object of the form{status: 'fulfilled', value: ...}or{status: 'rejected', reason: ...}. - Use
Promise.allto wait for all these wrapped promises to resolve. - Return the resulting array in the same order as the input.
Code
JavaScript
function promiseAllSettled(functions) {
return Promise.all(functions.map(fn =>
fn().then(
val => ({ status: "fulfilled", value: val }),
err => ({ status: "rejected", reason: err })
)
));
}
TypeScript
function promiseAllSettled(functions: (() => Promise<any>)[]): Promise<{status: string, value?: any, reason?: any}[]> {
return Promise.all(functions.map(fn =>
fn().then(
val => ({ status: "fulfilled", value: val }),
err => ({ status: "rejected", reason: err })
)
));
}
Complexity
- ⏰ Time complexity:
O(n), where n is the number of functions. Each promise is started and settled independently. - 🧺 Space complexity:
O(n), for the array of result objects.