2637. Promise Time Limit LeetCode Solution
In this guide, you will get 2637. Promise Time Limit LeetCode Solution with the best time and space complexity. The solution to Promise Time Limit problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Promise Time Limit solution in C++
- Promise Time Limit solution in Java
- Promise Time Limit solution in Python
- Additional Resources

Problem Statement of Promise Time Limit
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”.
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}
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:
fn = async (n) => {
await new Promise(res => setTimeout(res, 100));
return n * n;
}
inputs = [5]
t = 150
Output: {“resolved”:25,”time”:100}
Explanation:
The function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.
Example 3:
Input:
fn = async (a, b) => {
await new Promise(res => setTimeout(res, 120));
return a + b;
}
inputs = [5,10]
t = 150
Output: {“resolved”:15,”time”:120}
Explanation:
The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.
Example 4:
Input:
fn = async () => {
throw “Error”;
}
inputs = []
t = 1000
Output: {“rejected”:”Error”,”time”:0}
Explanation:
The function immediately throws an error.
Constraints:
0 <= inputs.length <= 10
0 <= t <= 1000
fn returns a promise
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
2637. Promise Time Limit LeetCode Solution in C++
type Fn = (...params: any[]) => Promise<any>;
function timeLimit(fn: Fn, t: number): Fn {
return async function (...args) {
const p1 = new Promise((_, reject) => {
setTimeout(() => reject('Time Limit Exceeded'), t);
});
const p2 = fn(...args);
return Promise.race([p1, p2]);
};
}
/* code provided by PROGIEZ */
2637. Promise Time Limit LeetCode Solution in Java
N/A
// code provided by PROGIEZ
2637. Promise Time Limit LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.