Skip to content

Instantly share code, notes, and snippets.

@Tribhuwan-Joshi
Created May 16, 2023 09:31
Show Gist options
  • Save Tribhuwan-Joshi/11b6cf273e2d2aadf6726d87b0a0e834 to your computer and use it in GitHub Desktop.
Save Tribhuwan-Joshi/11b6cf273e2d2aadf6726d87b0a0e834 to your computer and use it in GitHub Desktop.
Promise Time Limit JS
/**
* @param {Function} fn
* @param {number} t
* @return {Function}
*/
var timeLimit = function(fn, t) {
return async function(...args) {
return Promise.race([new Promise((res,rej)=>setTimeout(()=>rej("Time Limit Exceeded"),t)) , fn(...args) ])
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment