medium 0 views

Promise Time Limit

Given an asynchronous function fn and a time t in milliseconds, return a new time limited version of the input function.

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:

Example 1

Input:

Output: {"rejected":"Time Limit Exceeded","time":50}

Explanation: const limited = timeLimit(fn, t) const start = performance.now() let result; try { const res = await limited(...inputs) result = {"resolved": res, "time": Math.floor(performance.now() - start)}; } catch (err) { result = {"rejected": err, "time": Math.floor(performance.now() - start)}; } console.log(result) // Output The 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.

Example 2

Input:

Output: {"resolved":25,"time":100}

Explanation: The function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.

Example 3

Input:

Output: {"resolved":15,"time":120}

Explanation: ​​​​The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.

Example 4

Input:

Output: {"rejected":"Error","time":0}

Explanation: The function immediately throws an error.

Constraints

  • 0 <= inputs.length <= 10
  • 0 <= t <= 1000
  • fn returns a promise

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.