Skip to content

2637-promise-time-limit

DevGod
DevGod
Vtuber
/**
* @param {Function} fn
* @param {number} t
* @return {Function}
*/
var timeLimit = function(fn, t) {
return async function(...args) {
const originalFnPromise = fn(...args);
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject('Time Limit Exceeded')
}, t);
})
return Promise.race([originalFnPromise, timeoutPromise]);
}
};
/**
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
*/