Created
March 2, 2022 00:02
-
-
Save alecat88/71fd028669c8ba29b801a3a481a1e84c to your computer and use it in GitHub Desktop.
ES12
This file contains 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
// Create a Promise. | |
const promise1 = new Promise((resolve, reject) => { | |
// After 1 second reject the first promise. | |
setTimeout(() => reject("The first promise has been rejected."), 1000); | |
}); | |
// Create a Promise. | |
const promise2 = new Promise((resolve, reject) => { | |
// After 500 miliseconds reject the second promise. | |
setTimeout(() => reject("The second promise has been rejected."), 500); | |
}); | |
// Try executing the Promises. | |
(async function () { | |
try { | |
const data = await Promise.any([promise1, promise2]); | |
console.log(data); | |
} catch (error) { | |
// If all Promises gets rejected, then this try-catch block will handle | |
// the aggregate errors. | |
console.log("Error: ", error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment