Skip to content

Instantly share code, notes, and snippets.

@marioluevanos
Created January 11, 2019 21:59
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 marioluevanos/938cd6f6985044f04aaf3e66720416de to your computer and use it in GitHub Desktop.
Save marioluevanos/938cd6f6985044f04aaf3e66720416de to your computer and use it in GitHub Desktop.
A Promise with a timeout
function promiseWithTimeout(promise, timeoutTime = 5000) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject({
message: 'timeout'
}), timeoutTime)
const resolveWithTimeout = (res) => {
clearTimeout(timeout)
resolve(res)
}
const rejectWithTimeout = (err) => {
clearTimeout(timeout)
reject(err)
}
promise.then(resolveWithTimeout).catch(rejectWithTimeout)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment