Skip to content

Instantly share code, notes, and snippets.

Last active January 1, 2019 02:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
adds tracking of the promise timeout via `__timeout` flag
function promiseTimeout(msec) {
return (promise) => {
let isDone = false
promise.then(() => isDone = true)
const timeout = new Promise((yea, nah) => setTimeout(() => {
if (!isDone) {
promise.__timeout = true
nah(new Error('Timeout expired'))
}, msec))
return Promise.race([promise, timeout])
.then(response => response.json())
.then(data => {
console.log(data) // Prints result from `response.json()` in getRequest
.catch(error => console.error(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment