Skip to content

Instantly share code, notes, and snippets.

@fxp
Created September 13, 2018 08:35
Show Gist options
  • Save fxp/797d070ab10184cdde03608ddf539f8d to your computer and use it in GitHub Desktop.
Save fxp/797d070ab10184cdde03608ddf539f8d to your computer and use it in GitHub Desktop.
Timeout promise version and usage
function promiseTimeout(time) {
return new Promise((resolve, reject) => {
setTimeout(() => reject('timeout'), time);
});
};
let promiseB = new Promise((resolve, reject) => {
setTimeout(() => {
reject('Promise win!');
}, 400)
})
let race = Promise.race([
promiseTimeout(200),
promiseB
])
race.then((res) => console.log(res), (err) => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment