Skip to content

Practice Promise Time Limit

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2637
LeetCode Problem

Promise Time Limit

Problem Statement

Wrap asynchronous function fn with a t-millisecond deadline while forwarding the wrapper's arguments to fn. If fn settles within the time limit, preserve its resolved value or rejection. If it is still pending when the deadline is exceeded, reject with the string "Time Limit Exceeded".

Source: Promise Time Limit on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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}

# 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}

# 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}

# Example 4

Input
fn = async () => { throw "Error"; } inputs = [] t = 1000
Output
{"rejected":"Error","time":0}

Constraints

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

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.