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.
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().
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 =>{constout={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:
1
2
3
4
5
6
7
8
9
10
11
12
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
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 functionis 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.
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.
For each function in the input array, call it to get a promise.
For each promise, attach .then and .catch handlers to convert the result into an object of the form {status: 'fulfilled', value: ...} or {status: 'rejected', reason: ...}.
Use Promise.all to wait for all these wrapped promises to resolve.
Return the resulting array in the same order as the input.