Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Last active January 26, 2021 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montanaflynn/212387a9336487744e904fa2d2fbf200 to your computer and use it in GitHub Desktop.
Save montanaflynn/212387a9336487744e904fa2d2fbf200 to your computer and use it in GitHub Desktop.
Example of running all parallel promises with a race timeout promise in javascript
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 50, 'one');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 60, 'two');
});
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 70, 'three');
});
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(reject, 65, 'timed out');
});
const parallelPromise = Promise.all([promise1, promise2, promise3])
Promise
.race([parallelPromise, timeoutPromise])
.then((value) => {
console.log(value);
})
.catch((err)=>{
console.log(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment