Skip to content

Instantly share code, notes, and snippets.

@tlumko
Last active August 20, 2018 12:13
Show Gist options
  • Save tlumko/528aae1b57e30c13e1e412389284936e to your computer and use it in GitHub Desktop.
Save tlumko/528aae1b57e30c13e1e412389284936e to your computer and use it in GitHub Desktop.
Promise timeout
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