-
-
Save Jarvis1010/0f962833a126640b45078c36d0a46f0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wait(milliseconds) { | |
return new Promise((resolve) => setTimeout(resolve, milliseconds)) | |
} | |
export function withTimeout( | |
promiseFn, | |
opts, | |
){ | |
const timeOut = opts?.timeOut ?? 2500 | |
const errorMessage = | |
opts?.errorMessage ?? `Your Request timed out after ${timeOut}ms` | |
return (...args) => | |
Promise.race([ | |
promiseFn(...args), | |
wait(timeOut).then(() => { | |
throw new Error(errorMessage) | |
}), | |
]) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment