{ "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. All the promises should be executed in parallel.
promise resolves:
functions were resolved successfully in parallel. 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. The promise should resolve when all the asynchronous functions in the array have completed execution in parallel.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\nExample 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\nExample 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 promises1 <= functions.length <= 10给定一个异步函数数组 functions,返回一个新的 promise 对象 promise。数组中的每个函数都不接受参数并返回一个 promise。所有的 promise 都应该并行执行。
promise resolve 条件:
functions 返回的 promise 都成功的并行解析时。promise 的解析值应该是一个按照它们在 functions 中的顺序排列的 promise 的解析值数组。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 是一个返回 promise 的函数数组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 \\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js <\\/a>\\u5e93\\u6240\\u63d0\\u4f9b\\u7684\\u6570\\u636e\\u7ed3\\u6784\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.7.3<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\nNode.js 22.14.0<\\/code><\\/p>\\r\\n\\r\\n--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n