Last active
August 20, 2018 12:13
-
-
Save tlumko/528aae1b57e30c13e1e412389284936e to your computer and use it in GitHub Desktop.
Promise timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function promiseTimeout(promise, ms){ | |
let timerId; | |
let timeout = new Promise((resolve, reject) => { | |
timerId = setTimeout(() => reject('timeout'), ms); | |
}); | |
return Promise.race([ | |
timeout, | |
promise.then(r => { | |
clearTimeout(timerId); | |
return r; | |
}), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment