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:
If the fn completes within the time limit of t milliseconds, the time limited function should resolve with the result.
If the execution of the fn exceeds the time limit, the time limited function should reject with the string "Time Limit Exceeded".
Input:
fn = async (n)=>{ await newPromise(res => setTimeout(res,100));return n * n;}inputs =[5]t =50Output: {"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 functionis set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.
Input:
fn = async (n)=>{ await newPromise(res => setTimeout(res,100));return n * n;}inputs =[5]t =150Output: {"resolved":25,"time":100}Explanation:
The function resolved 5*5=25 at t=100ms. The time limit is never reached.
Input:
fn = async (a, b)=>{ await newPromise(res => setTimeout(res,120));return a + b;}inputs =[5,10]t =150Output: {"resolved":15,"time":120}Explanation:
The function resolved 5+10=15 at t=120ms. The time limit is never reached.#### Example 4Input:
fn = async ()=>{throw"Error";}inputs =[]t =1000Output: {"rejected":"Error","time":0}Explanation:
The function immediately throws an error.
We want to run the original async function, but if it takes longer than t milliseconds, we should reject with “Time Limit Exceeded”. This is a classic use case for Promise.race, where we race the original promise against a timeout promise.