Skip to content

Instantly share code, notes, and snippets.

@romanonthego
Created July 3, 2017 09:09
Show Gist options
  • Save romanonthego/104bc1f2aab0e2748df18672059dfaff to your computer and use it in GitHub Desktop.
Save romanonthego/104bc1f2aab0e2748df18672059dfaff to your computer and use it in GitHub Desktop.
export 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