{ "data": { "question": { "questionId": "2749", "questionFrontendId": "2637", "categoryTitle": "JavaScript", "boundTopicId": 2222267, "title": "Promise Time Limit", "titleSlug": "promise-time-limit", "content": "
Given an asynchronous function fn
and a time t
in milliseconds, return a new time limited version of the input function. fn
takes arguments provided to the time limited function.
The time limited function should follow these rules:
\n\nfn
completes within the time limit of t
milliseconds, the time limited function should resolve with the result.fn
exceeds the time limit, the time limited function should reject with the string "Time Limit Exceeded"
.\n
Example 1:
\n\n\nInput: \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 50\nOutput: {"rejected":"Time Limit Exceeded","time":50}\nExplanation:\nconst limited = timeLimit(fn, t)\nconst start = performance.now()\nlet result;\ntry {\n const res = await limited(...inputs)\n result = {"resolved": res, "time": Math.floor(performance.now() - start)};\n} catch (err) {\n result = {"rejected": err, "time": Math.floor(performance.now() - start)};\n}\nconsole.log(result) // Output\n\nThe provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.\n\n\n
Example 2:
\n\n\nInput: \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 150\nOutput: {"resolved":25,"time":100}\nExplanation:\nThe function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.\n\n\n
Example 3:
\n\n\nInput: \nfn = async (a, b) => { \n await new Promise(res => setTimeout(res, 120)); \n return a + b; \n}\ninputs = [5,10]\nt = 150\nOutput: {"resolved":15,"time":120}\nExplanation:\nThe function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.\n\n\n
Example 4:
\n\n\nInput: \nfn = async () => { \n throw "Error";\n}\ninputs = []\nt = 1000\nOutput: {"rejected":"Error","time":0}\nExplanation:\nThe function immediately throws an error.\n\n
\n
Constraints:
\n\n0 <= inputs.length <= 10
0 <= t <= 1000
fn
returns a promise请你编写一个函数,它接受一个异步函数 fn
和一个以毫秒为单位的时间 t
。它应根据限时函数返回一个有 限时 效果的函数。函数 fn
接受提供给 限时 函数的参数。
限时 函数应遵循以下规则:
\n\nfn
在 t
毫秒的时间限制内完成,限时 函数应返回结果。fn
的执行超过时间限制,限时 函数应拒绝并返回字符串 \"Time Limit Exceeded\"
。\n\n
示例 1:
\n\n\n输入:\nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 50\n输出:{\"rejected\":\"Time Limit Exceeded\",\"time\":50}\n解释:\nconst limited = timeLimit(fn, t)\nconst start = performance.now()\nlet result;\ntry {\n const res = await limited(...inputs)\n result = {\"resolved\": res, \"time\": Math.floor(performance.now() - start)};\n} catch (err) {\n result = {\"rejected\": err, \"time\": Math.floor(performance.now() - start)};\n}\nconsole.log(result) // 输出结果\n\n提供的函数设置在 100ms 后执行完成,但是设置的超时时间为 50ms,所以在 t=50ms 时拒绝因为达到了超时时间。\n\n\n
示例 2:
\n\n\n输入:\nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 150\n输出:{\"resolved\":25,\"time\":100}\n解释:\n在 t=100ms 时执行 5*5=25 ,没有达到超时时间。\n\n\n
示例 3:
\n\n\n输入:\nfn = async (a, b) => { \n await new Promise(res => setTimeout(res, 120)); \n return a + b; \n}\ninputs = [5,10]\nt = 150\n输出:{\"resolved\":15,\"time\":120}\n解释:\n在 t=120ms 时执行 5+10=15,没有达到超时时间。\n\n\n
示例 4:
\n\n\n输入:\nfn = async () => { \n throw \"Error\";\n}\ninputs = []\nt = 1000\n输出:{\"rejected\":\"Error\",\"time\":0}\n解释:\n此函数始终丢出 Error\n\n
\n\n
提示:
\n\n0 <= inputs.length <= 10
0 <= t <= 1000
fn
返回一个 Promise 对象\\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 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/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": "async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n50\nasync (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n150\nasync (a, b) => { await new Promise(res => setTimeout(res, 120)); return a + b; }\n[5,10]\n150\nasync () => { throw \"Error\"; }\n[]\n1000",
"__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