Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Created July 9, 2019 21:24
Show Gist options
  • Save jmakeig/937385e8ae3e3eb8a4e11dc49d47d718 to your computer and use it in GitHub Desktop.
Save jmakeig/937385e8ae3e3eb8a4e11dc49d47d718 to your computer and use it in GitHub Desktop.
Promise timeout
// https://github.com/github/fetch/issues/175#issuecomment-216791333
function timeoutPromise(ms, promise) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('promise timeout'));
}, ms);
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