mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
59 lines
11 KiB
JSON
59 lines
11 KiB
JSON
{
|
||
"data": {
|
||
"question": {
|
||
"questionId": "2807",
|
||
"questionFrontendId": "2721",
|
||
"categoryTitle": "JavaScript",
|
||
"boundTopicId": 2302685,
|
||
"title": "Execute Asynchronous Functions in Parallel",
|
||
"titleSlug": "execute-asynchronous-functions-in-parallel",
|
||
"content": "<p>Given an array of asynchronous functions <code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments and returns a promise. All the promises should be executed in parallel.</p>\n\n<p><code>promise</code> resolves:</p>\n\n<ul>\n\t<li>When all the promises returned from <code>functions</code> were resolved successfully in parallel. The resolved value of <code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the <code>functions</code>. The <code>promise</code> should resolve when all the asynchronous functions in the array have completed execution in parallel.</li>\n</ul>\n\n<p><code>promise</code> rejects:</p>\n\n<ul>\n\t<li>When any of the promises returned from <code>functions</code> were rejected. <code>promise</code> should also reject with the reason of the first rejection.</li>\n</ul>\n\n<p>Please solve it without using the built-in <code>Promise.all</code> function.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n () => new Promise(resolve => setTimeout(() => resolve(5), 200))\n]\n<strong>Output:</strong> {"t": 200, "resolved": [5]}\n<strong>Explanation:</strong> \npromiseAll(functions).then(console.log); // [5]\n\nThe single function was resolved at 200ms with a value of 5.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n () => new Promise(resolve => setTimeout(() => resolve(1), 200)), \n () => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100))\n]\n<strong>Output:</strong> {"t": 100, "rejected": "Error"}\n<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> 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<strong>Output:</strong> {"t": 150, "resolved": [4, 10, 16]}\n<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>functions</code> is an array of functions that returns promises</li>\n\t<li><code>1 <= functions.length <= 10</code></li>\n</ul>\n",
|
||
"translatedTitle": "并行执行异步函数",
|
||
"translatedContent": "<p>给定一个异步函数数组 <code>functions</code>,返回一个新的 promise 对象 <code>promise</code>。数组中的每个函数都不接受参数并返回一个 promise。所有的 promise 都应该并行执行。</p>\n\n<p><code>promise</code> resolve 条件:</p>\n\n<ul>\n\t<li>当所有从 <code>functions</code> 返回的 promise 都成功的并行解析时。<code>promise</code> 的解析值应该是一个按照它们在 <code>functions</code> 中的顺序排列的 promise 的解析值数组。<code>promise</code> 应该在数组中的所有异步函数并行执行完成时解析。</li>\n</ul>\n\n<p><code>promise</code> reject 条件:</p>\n\n<ul>\n\t<li>当任何从 <code>functions</code> 返回的 promise 被拒绝时。<code>promise</code> 也会被拒绝,并返回第一个拒绝的原因。</li>\n</ul>\n\n<p>请在不使用内置的 <code>Promise.all</code> 函数的情况下解决。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>functions = [\n () => new Promise(resolve => setTimeout(() => resolve(5), 200))\n]\n<b>输出:</b>{\"t\": 200, \"resolved\": [5]}\n<b>解释:</b>\npromiseAll(functions).then(console.log); // [5]\n\n单个函数在 200 毫秒后以值 5 成功解析。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>functions = [\n () => new Promise(resolve => setTimeout(() => resolve(1), 200)), \n () => new Promise((resolve, reject) => setTimeout(() => reject(\"Error\"), 100))\n]\n<b>输出:</b>{\"t\": 100, \"rejected\": \"Error\"}\n<b>解释:</b>由于其中一个 promise 被拒绝,返回的 promise 也在同一时间被拒绝并返回相同的错误。\n</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>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<b>输出:</b>{\"t\": 150, \"resolved\": [4, 10, 16]}\n<b>解释:</b>所有的 promise 都成功执行。当最后一个 promise 被解析时,返回的 promise 也被解析了。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li>函数 <code>functions</code> 是一个返回 promise 的函数数组</li>\n\t<li><code>1 <= functions.length <= 10</code></li>\n</ul>\n",
|
||
"isPaidOnly": false,
|
||
"difficulty": "Medium",
|
||
"likes": 5,
|
||
"dislikes": 0,
|
||
"isLiked": null,
|
||
"similarQuestions": "[]",
|
||
"contributors": [],
|
||
"langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}",
|
||
"topicTags": [],
|
||
"companyTagStats": null,
|
||
"codeSnippets": [
|
||
{
|
||
"lang": "JavaScript",
|
||
"langSlug": "javascript",
|
||
"code": "/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = function(functions) {\n \n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "TypeScript",
|
||
"langSlug": "typescript",
|
||
"code": "type Fn<T> = () => Promise<T>\n\nfunction promiseAll<T>(functions: Fn<T>[]): Promise<T[]> {\n\t\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */",
|
||
"__typename": "CodeSnippetNode"
|
||
}
|
||
],
|
||
"stats": "{\"totalAccepted\": \"1.9K\", \"totalSubmission\": \"3.3K\", \"totalAcceptedRaw\": 1901, \"totalSubmissionRaw\": 3309, \"acRate\": \"57.4%\"}",
|
||
"hints": [],
|
||
"solution": null,
|
||
"status": null,
|
||
"sampleTestCase": "[() => new Promise(resolve => setTimeout(() => resolve(5), 200))]",
|
||
"metaData": "{\n \"name\": \"promiseAll\",\n \"params\": [\n {\n \"name\": \"functions\",\n \"type\": \"string[]\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||
"judgerAvailable": true,
|
||
"judgeType": "large",
|
||
"mysqlSchemas": [],
|
||
"enableRunCode": true,
|
||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> 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"
|
||
}
|
||
}
|
||
} |