Skip to content

Instantly share code, notes, and snippets.

@Jarvis1010
Last active July 29, 2021 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jarvis1010/0f962833a126640b45078c36d0a46f0d to your computer and use it in GitHub Desktop.
Save Jarvis1010/0f962833a126640b45078c36d0a46f0d to your computer and use it in GitHub Desktop.
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