Skip to content

Instantly share code, notes, and snippets.

@alecat88
Created March 2, 2022 00:04
Show Gist options
  • Save alecat88/2a55ac3d22bf7edad14bec72bb2c34a8 to your computer and use it in GitHub Desktop.
Save alecat88/2a55ac3d22bf7edad14bec72bb2c34a8 to your computer and use it in GitHub Desktop.
ES12
// Create a Promise.
const promise1 = new Promise((resolve, reject) => {
// After 2 seconds resolve the first promise.
setTimeout(() => resolve("The first promise has been resolved."), 2000);
});
// Create a Promise.
const promise2 = new Promise((resolve, reject) => {
// After 1 second resolve the second promise.
setTimeout(() => resolve("The second promise has been resolved."), 1000);
});
// Create a Promise.
const promise3 = new Promise((resolve, reject) => {
// After 3 seconds resolve the third promise.
setTimeout(() => resolve("The third promise has been resolved."), 3000);
});
(async function () {
const data = await Promise.any([promise1, promise2, promise3]);
// Print the data returned from the first resolved Promise.
console.log(data);
// The above will print: The second promise has been resolved.
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment