2637. Promise Time Limit
https://leetcode.com/problems/promise-time-limit/
Javascript
var timeLimit = function(fn, t) {
return async function(...args) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject("Time Limit Exceeded")
}, t)
fn(...args)
.then((result) => resolve(result))
.catch((error) => reject(error))
.finally(() => clearTimeout(timer))
})
}
}
Typescript
type Fn = (...params: any[]) => Promise<any>;
function timeLimit(fn: Fn, t: number): Fn {
return async function(...args) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject("Time Limit Exceeded")
}, t)
fn(...args)
.then((result) => resolve(result))
.catch((error) => reject(error))
.finally(() => clearTimeout(timer))
})
}
};