{ "data": { "question": { "questionId": "2807", "questionFrontendId": "2721", "categoryTitle": "JavaScript", "boundTopicId": 2302685, "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
给定一个异步函数数组 functions
,返回一个新的 promise 对象 promise
。数组中的每个函数都不接受参数并返回一个 promise。
promise
resolve 条件:
functions
返回的 promise 都成功解析时。promise
的解析值应该是一个按照它们在 functions
中的顺序排列的 promise 的解析值数组。promise
reject 条件:
functions
返回的 promise 被拒绝时。promise
也会被拒绝,并返回第一个拒绝的原因。请在不使用内置的 Promise.all
函数的情况下解决。
\n\n
示例 1:
\n\n\n输入:functions = [\n () => new Promise(resolve => setTimeout(() => resolve(5), 200))\n]\n输出:{\"t\": 200, \"resolved\": [5]}\n解释:\npromiseAll(functions).then(console.log); // [5]\n\n单个函数在 200 毫秒后以值 5 成功解析。\n\n\n
示例 2:
\n\n\n输入:functions = [\n () => new Promise(resolve => setTimeout(() => resolve(1), 200)), \n () => new Promise((resolve, reject) => setTimeout(() => reject(\"Error\"), 100))\n]\n输出:{\"t\": 100, \"rejected\": \"Error\"}\n解释:由于其中一个 promise 被拒绝,返回的 promise 也在同一时间被拒绝并返回相同的错误。\n\n\n
示例 3:
\n\n\n输入: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]\n输出:{\"t\": 150, \"resolved\": [4, 10, 16]}\n解释:所有的 promise 都成功执行。当最后一个 promise 被解析时,返回的 promise 也被解析了。\n\n\n
\n\n
提示:
\n\nfunctions is an array of functions that returns promises
1 <= functions.length <= 10
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 4.5.4<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[() => new Promise(resolve => setTimeout(() => resolve(5), 200))]\n[() => new Promise(resolve => setTimeout(() => resolve(1), 200)), () => new Promise((resolve, reject) => setTimeout(() => reject(\"Error\"), 100))]\n[() => new Promise(resolve => setTimeout(() => resolve(4), 50)), () => new Promise(resolve => setTimeout(() => resolve(10), 150)), () => new Promise(resolve => setTimeout(() => resolve(16), 100))]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n