Skip to content

Instantly share code, notes, and snippets.

@juice49
Created October 6, 2016 15:20
Show Gist options
  • Save juice49/5530926d691aa18629e988bef772dd54 to your computer and use it in GitHub Desktop.
Save juice49/5530926d691aa18629e988bef772dd54 to your computer and use it in GitHub Desktop.
Timeout a JS promise
'use strict';
const timeoutPromise = ms => promise => new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new PromiseTimeout());
}, ms);
promise.then(
res => {
clearTimeout(timeout);
resolve(res);
},
err => {
clearTimeout(timeout);
reject(err);
}
);
});
export default timeoutPromise;
export class PromiseTimeout {
constructor() {
this.name = 'PromiseTimeout';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment