{ "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.
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.
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.
You can assume all functions
never reject. It is acceptable for promisePool
to return a promise that resolves any value.
\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\n0 <= functions.length <= 10
1 <= n <= 10
请你编写一个异步函数 promisePool
,它接收一个异步函数数组 functions
和 池限制 n
。它应该返回一个 promise 对象,当所有输入函数都执行完毕后,promise 对象就执行完毕。
池限制 定义是一次可以挂起的最多 promise 对象的数量。promisePool
应该开始执行尽可能多的函数,并在旧的 promise 执行完毕后继续执行新函数。promisePool
应该先执行 functions[i]
,再执行 functions[i + 1]
,然后执行 functions[i + 2]
,等等。当最后一个 promise 执行完毕时,promisePool
也应该执行完毕。
例如,如果 n = 1
, promisePool
在序列中每次执行一个函数。然而,如果 n = 2
,它首先执行两个函数。当两个函数中的任何一个执行完毕后,再执行第三个函数(如果它是可用的),依此类推,直到没有函数要执行为止。
你可以假设所有的 functions
都不会被拒绝。对于 promisePool
来说,返回一个可以解析任何值的 promise 都是可以接受的。
\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\n0 <= functions.length <= 10
1 <= n <= 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(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"
}
}
}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