"content":"<p>Given an asynchronous function <code>fn</code> and a time <code>t</code> in milliseconds, return a new <strong>time limited</strong> version of the input function. <code>fn</code> takes arguments provided to the <strong>time limited </strong>function.</p>\n\n<p>The <strong>time limited</strong> function should follow these rules:</p>\n\n<ul>\n\t<li>If the <code>fn</code> completes within the time limit of <code>t</code> milliseconds, the <strong>time limited</strong> function should resolve with the result.</li>\n\t<li>If the execution of the <code>fn</code> exceeds the time limit, the <strong>time limited</strong> function should reject with the string <code>"Time Limit Exceeded"</code>.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 50\n<strong>Output:</strong> {"rejected":"Time Limit Exceeded","time":50}\n<strong>Explanation:</strong>\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</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 150\n<strong>Output:</strong> {"resolved":25,"time":100}\n<strong>Explanation:</strong>\nThe function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (a, b) => { \n await new Promise(res => setTimeout(res, 120)); \n return a + b; \n}\ninputs = [5,10]\nt = 150\n<strong>Output:</strong> {"resolved":15,"time":120}\n<strong>Explanation:</strong>\nThe function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async () => { \n throw "Error";\n}\ninputs = []\nt = 1000\n<strong>Output:</strong> {"rejected":"Error","time":0}\n<strong>Explanation:</strong>\nThe function immediately throws an error.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= inputs.length <= 10</code></li>\n\t<li><code>0 <= t <= 1000</code></li>\n\t<li><code>fn</code> returns a promise</li>\n</ul>\n",
"You can return a copy of a function with: \r\n\r\nfunction outerFunction(fn) { \r\n return function innerFunction(...params) {\r\n return fn(...params);\r\n };\r\n}",
"Inside the inner function, you will need to return a new Promise.",
"You can create a new promise like: new Promise((resolve, reject) => {}).",
"You can execute code with a delay with \"setTimeout(fn, delay)\"",
"To reject a promise after a delay, \"setTimeout(() => reject('err'), delay)\"",
"You can resolve and reject when the passed promise resolves or rejects with: \"fn(...params).then(resolve).catch(reject)\""
],
"solution":null,
"status":null,
"sampleTestCase":"async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n50",