g2601_2700.s2637_promise_time_limit.solution.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
// #Medium #2023_09_01_Time_57_ms_(84.99%)_Space_43_MB_(45.71%)
type Fn = (...params: any[]) => Promise
function timeLimit(fn: Fn, t: number): Fn {
return async function (...args) {
const timeout = new Promise((_, reject) => {
setTimeout(() => {
reject('Time Limit Exceeded') //NOSONAR
}, t)
})
return Promise.race([fn(...args), timeout])
}
}
/*
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
*/
export { timeLimit }