{ "data": { "question": { "questionId": "2807", "questionFrontendId": "2721", "boundTopicId": null, "title": "Execute Asynchronous Functions in Parallel", "titleSlug": "execute-asynchronous-functions-in-parallel", "content": "
Given an array of asynchronous functions functions
, return a new promise promise
. Each function in the array accepts no arguments and returns a promise.
promise
resolves:
functions
were resolved successfully. The resolved value of promise
should be an array of all the resolved values of promises in the same order as they were in the functions.
promise
rejects:
functions
were rejected. promise
should also reject with the reason of the first rejection.Please solve it without using the built-in Promise.all
function.
\n
Example 1:
\n\n\nInput: functions = [\n () => new Promise(resolve => setTimeout(() => resolve(5), 200))\n]\nOutput: {"t": 200, "resolved": [5]}\nExplanation: \npromiseAll(functions).then(console.log); // [5]\n\nThe single function was resolved at 200ms with a value of 5.\n\n\n
Example 2:
\n\n\nInput: functions = [\n () => new Promise(resolve => setTimeout(() => resolve(1), 200)), \n () => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))\n]\nOutput: {"t": 100, "rejected": "Error"}\nExplanation: Since one of the promises rejected, the returned promise also rejected with the same error at the same time.\n\n\n
Example 3:
\n\n\nInput: functions = [\n () => new Promise(resolve => setTimeout(() => resolve(4), 50)), \n () => new Promise(resolve => setTimeout(() => resolve(10), 150)), \n () => new Promise(resolve => setTimeout(() => resolve(16), 100))\n]\nOutput: {"t": 150, "resolved": [4, 10, 16]}\nExplanation: All the promises resolved with a value. The returned promise resolved when the last promise resolved.\n\n\n
\n
Constraints:
\n\nfunctions is an array of functions that returns promises
1 <= functions.length <= 10
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }