Skip to content

Instantly share code, notes, and snippets.

@Jarvis1010
Last active Jul 29, 2021
Embed
What would you like to do?
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