Last active
January 26, 2021 03:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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