Skip to content

Instantly share code, notes, and snippets.

@garystorey
Created March 28, 2018 22:32
Show Gist options
  • Save garystorey/95b6b5394d034c48aedd5150892217bf to your computer and use it in GitHub Desktop.
Save garystorey/95b6b5394d034c48aedd5150892217bf to your computer and use it in GitHub Desktop.
promise based timeout
/* https://github.com/github/fetch/issues/175 */
function timeout(ms, promise) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error("timeout"))
}, ms)
promise.then(resolve, reject)
})
}
/*Usage*/
timeout(1000, fetch('/hello')).then(function(response) {
// process response
}).catch(function(error) {
// might be a timeout error
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment