Skip to main content
Back to problems
#2637
Medium JavaScript

Promise time limit

83.2% acceptance
Mar 2, 2026
574
85
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".

Solution

TypeScript
Time O(1)
Space O(1)
LeetCode
solution.ts
type Fn = (...params: any[]) => Promise<any>;

function timeLimit(fn: Fn, t: number): Fn {
  return async function (...args: any[]): Promise<any> {
  const timeout = new Promise<never>((_, reject) =>
    setTimeout(() => reject("Time Limit Exceeded"), t),
  );
  return Promise.race([fn(...args), timeout]);
  };
}

/**
 * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
 * limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
 */