Skip to content

Commit a03757b

Browse files
committedJan 6, 2025
Add solution #2637
1 parent 5624641 commit a03757b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
2627|[Debounce](./2627-debounce.js)|Medium|
387387
2629|[Function Composition](./2629-function-composition.js)|Easy|
388388
2630|[Memoize II](./2630-memoize-ii.js)|Hard|
389+
2637|[Promise Time Limit](./2637-promise-time-limit.js)|Medium|
389390
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
390391
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
391392
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|

‎solutions/2637-promise-time-limit.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 2637. Promise Time Limit
3+
* https://leetcode.com/problems/promise-time-limit/
4+
* Difficulty: Medium
5+
*
6+
* Given an asynchronous function fn and a time t in milliseconds, return a new time limited
7+
* version of the input function. fn takes arguments provided to the time limited function.
8+
*
9+
* The time limited function should follow these rules:
10+
* - If the fn completes within the time limit of t milliseconds, the time limited function
11+
* should resolve with the result.
12+
* - If the execution of the fn exceeds the time limit, the time limited function should
13+
* reject with the string "Time Limit Exceeded".
14+
*/
15+
16+
/**
17+
* @param {Function} fn
18+
* @param {number} t
19+
* @return {Function}
20+
*/
21+
var timeLimit = function(fn, t) {
22+
return async function(...args) {
23+
return Promise.race([
24+
fn(...args),
25+
new Promise((_, reject) => setTimeout(() => reject('Time Limit Exceeded'), t)),
26+
]);
27+
};
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.