LeetCode

2637. Promise Time Limit

Medium
July 21, 2025 · Asynchronous · Promises · leetcode.com

Problem

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: fn = async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; },
       inputs = [5], t = 50
Output: { rejected: "Time Limit Exceeded", time: 50 }

Solution

Promise.race resolves or rejects with whichever promise settles first. So we race the real work against a “timer” promise that rejects after t milliseconds: if fn finishes first, we get its result; if the timer fires first, we get the rejection.

var timeLimit = function (fn, t) {
  return async function (...args) {
    return Promise.race([
      fn(...args),
      new Promise((_, reject) =>
        setTimeout(() => reject("Time Limit Exceeded"), t),
      ),
    ]);
  };
};

Complexity Analysis

Measure Complexity
Time Complexity O(1)
Space Complexity O(1)

Notes