{ "data": { "question": { "questionId": "2750", "questionFrontendId": "2636", "categoryTitle": "JavaScript", "boundTopicId": 2222268, "title": "Promise Pool", "titleSlug": "promise-pool", "content": "

Given an array of asyncronous functions functions and a pool limit n, return an asyncronous function promisePool. It should return a promise that resolves when all the input functions resolve.

\n\n

Pool limit is defined as the maximum number promises that can be pending at once. promisePool should begin execution of as many functions as possible and continue executing new functions when old promises resolve. promisePool should execute functions[i] then functions[i + 1] then functions[i + 2], etc. When the last promise resolves, promisePool should also resolve.

\n\n

For example, if n = 1, promisePool will execute one function at a time in series. However, if n = 2, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.

\n\n

You can assume all functions never reject. It is acceptable for promisePool to return a promise that resolves any value.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nfunctions = [\n  () => new Promise(res => setTimeout(res, 300)),\n  () => new Promise(res => setTimeout(res, 400)),\n  () => new Promise(res => setTimeout(res, 200))\n]\nn = 2\nOutput: [[300,400,500],500]\nExplanation:\nThree functions are passed in. They sleep for 300ms, 400ms, and 200ms respectively.\nAt t=0, the first 2 functions are executed. The pool size limit of 2 is reached.\nAt t=300, the 1st function resolves, and the 3rd function is executed. Pool size is 2.\nAt t=400, the 2nd function resolves. There is nothing left to execute. Pool size is 1.\nAt t=500, the 3rd function resolves. Pool size is zero so the returned promise also resolves.\n
\n\n

Example 2:

\n\n
\nInput:\nfunctions = [\n  () => new Promise(res => setTimeout(res, 300)),\n  () => new Promise(res => setTimeout(res, 400)),\n  () => new Promise(res => setTimeout(res, 200))\n]\nn = 5\nOutput: [[300,400,200],400]\nExplanation:\nAt t=0, all 3 functions are executed. The pool limit of 5 is never met.\nAt t=200, the 3rd function resolves. Pool size is 2.\nAt t=300, the 1st function resolved. Pool size is 1.\nAt t=400, the 2nd function resolves. Pool size is 0, so the returned promise also resolves.\n
\n\n

Example 3:

\n\n
\nInput:\nfunctions = [\n  () => new Promise(res => setTimeout(res, 300)),\n  () => new Promise(res => setTimeout(res, 400)),\n  () => new Promise(res => setTimeout(res, 200))\n]\nn = 1\nOutput: [[300,700,900],900]\nExplanation:\nAt t=0, the 1st function is executed. Pool size is 1.\nAt t=300, the 1st function resolves and the 2nd function is executed. Pool size is 1.\nAt t=700, the 2nd function resolves and the 3rd function is executed. Pool size is 1.\nAt t=900, the 3rd function resolves. Pool size is 0 so the returned promise resolves.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": "Promice 对象池", "translatedContent": "

请你编写一个异步函数 promisePool ,它接收一个异步函数数组 functions池限制 n。它应该返回一个 promise 对象,当所有输入函数都执行完毕后,promise 对象就执行完毕。

\n\n

池限制 定义是一次可以挂起的最多 promise 对象的数量。promisePool 应该开始执行尽可能多的函数,并在旧的 promise 执行完毕后继续执行新函数。promisePool 应该先执行 functions[i],再执行 functions[i + 1],然后执行 functions[i + 2],等等。当最后一个 promise 执行完毕时,promisePool 也应该执行完毕。

\n\n

例如,如果 n = 1 , promisePool 在序列中每次执行一个函数。然而,如果 n = 2 ,它首先执行两个函数。当两个函数中的任何一个执行完毕后,再执行第三个函数(如果它是可用的),依此类推,直到没有函数要执行为止。

\n\n

你可以假设所有的 functions 都不会被拒绝。对于 promisePool 来说,返回一个可以解析任何值的 promise 都是可以接受的。

\n\n

 

\n\n

示例 1:

\n\n
\n输入:\nfunctions = [\n  () => new Promise(res => setTimeout(res, 300)),\n  () => new Promise(res => setTimeout(res, 400)),\n  () => new Promise(res => setTimeout(res, 200))\n]\nn = 2\n输出:[[300,400,500],500]\n解释\n传递了三个函数。它们的睡眠时间分别为 300ms、 400ms 和 200ms。\n在 t=0 时,执行前两个函数。池大小限制达到 2。\n当 t=300 时,第一个函数执行完毕后,执行第3个函数。池大小为 2。\n在 t=400 时,第二个函数执行完毕后。没有什么可执行的了。池大小为 1。\n在 t=500 时,第三个函数执行完毕后。池大小为 0,因此返回的 promise 也执行完成。\n
\n\n

示例 2:

\n\n
\n输入:\nfunctions = [\n  () => new Promise(res => setTimeout(res, 300)),\n  () => new Promise(res => setTimeout(res, 400)),\n  () => new Promise(res => setTimeout(res, 200))\n]\nn = 5\n输出:[[300,400,200],400]\n解释:\n在 t=0 时,所有3个函数都被执行。池的限制大小 5 永远不会满足。\n在 t=200 时,第三个函数执行完毕后。池大小为 2。\n在 t=300 时,第一个函数执行完毕后。池大小为 1。\n在 t=400 时,第二个函数执行完毕后。池大小为 0,因此返回的 promise 也执行完成。\n
\n\n

示例 3:

\n\n
\n输入:\nfunctions = [\n  () => new Promise(res => setTimeout(res, 300)),\n  () => new Promise(res => setTimeout(res, 400)),\n  () => new Promise(res => setTimeout(res, 200))\n]\nn = 1\n输出:[[300,700,900],900]\n解释:\n在 t=0 时,执行第一个函数。池大小为1。\n当 t=300 时,第一个函数执行完毕后,执行第二个函数。池大小为 1。\n当 t=700 时,第二个函数执行完毕后,执行第三个函数。池大小为 1。\n在 t=900 时,第三个函数执行完毕后。池大小为 0,因此返回的 Promise 也执行完成。\n
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 2, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Function[]} functions\n * @param {number} n\n * @return {Function}\n */\nvar promisePool = async function(functions, n) {\n\n};\n\n/**\n * const sleep = (t) => new Promise(res => setTimeout(res, t));\n * promisePool([() => sleep(500), () => sleep(400)], 1)\n * .then(console.log) // After 900ms\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type F = () => Promise;\n\nfunction promisePool(functions: F[], n: number): Promise {\n\n};\n\n/**\n * const sleep = (t) => new Promise(res => setTimeout(res, t));\n * promisePool([() => sleep(500), () => sleep(400)], 1)\n * .then(console.log) // After 900ms\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"112\", \"totalSubmission\": \"154\", \"totalAcceptedRaw\": 112, \"totalSubmissionRaw\": 154, \"acRate\": \"72.7%\"}", "hints": [ "Initially execute all the functions until the queue fills up.", "Every time a function resolves, add a new promise to the queue if possible." ], "solution": null, "status": null, "sampleTestCase": "[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n2", "metaData": "{\n \"name\": \"promisePool\",\n \"params\": [\n {\n \"name\": \"getFunctions\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"n\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/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>\"],\"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(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n2\n[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n5\n[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n1", "__typename": "QuestionNode" } } }