Skip to content

Instantly share code, notes, and snippets.

@yezhiming
Created September 26, 2016 10:28
Show Gist options
  • Save yezhiming/8d08f0fe5faf15d9c449c7f05d0f7ef2 to your computer and use it in GitHub Desktop.
Save yezhiming/8d08f0fe5faf15d9c449c7f05d0f7ef2 to your computer and use it in GitHub Desktop.
timeout promise use with window.fetch
// ref: https://github.com/github/fetch/issues/175
function timeoutPromise(ms, promise) {
return new Promise( (resolve, reject) => {
const timeoutId = setTimeout( () => {
reject( new Error("timeout") )
});
promise.then(
(res) => {
clearTimeout(timeoutId);
resolve(res);
},
(err) => {
clearTimeout(timeoutId);
reject(err);
}
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment