Skip to content

Instantly share code, notes, and snippets.

@filipeandre
Last active May 27, 2024 11:15
Show Gist options
  • Save filipeandre/1a589439f304968300ba27b3ed91af71 to your computer and use it in GitHub Desktop.
Save filipeandre/1a589439f304968300ba27b3ed91af71 to your computer and use it in GitHub Desktop.
Examples of ways to wait for promises
// Promise.all only resolves when all Promises in the array passed in are resolved, and returns the array of resolved values.
const { setTimeout } = require('timers/promises');
const promise1 = setTimeout(100, 'Promise 1 resolved');
const promise2 = setTimeout(200, 'Promise 2 resolved');
const promise3 = setTimeout(300, 'Promise 3 resolved');
async function examplePromiseAll() {
try {
const results = await Promise.all([promise1, promise2, promise3]);
console.log(results); // Expected output: ["Promise 1 resolved", "Promise 2 resolved", "Promise 3 resolved"]
} catch (error) {
console.error(error);
}
}
examplePromiseAll();
// Promise.all If any Promise inside the iterator rejects, Promise.all rejects.
const { setTimeout } = require('timers/promises');
const promise1 = setTimeout(100, 'Promise 1 resolved');
const promise2 = setTimeout(200).then(() => { throw new Error('Promise 2 rejected'); });
const promise3 = setTimeout(300, 'Promise 3 resolved');
async function examplePromiseAllWithRejection() {
try {
const results = await Promise.all([promise1, promise2, promise3]);
console.log(results);
} catch (error) {
console.error(error.message); // Expected output: "Promise 2 rejected"
}
}
examplePromiseAllWithRejection();
// Promise.any is settled as soon as any of the promises you feed it is fulfilled or they are all rejected.
const { setTimeout } = require('timers/promises');
const promise1 = setTimeout(100).then(() => { throw new Error('Promise 1 rejected'); });
const promise2 = setTimeout(200, 'Promise 2 resolved');
const promise3 = setTimeout(300).then(() => { throw new Error('Promise 3 rejected'); });
async function examplePromiseAny() {
try {
const result = await Promise.any([promise1, promise2, promise3]);
console.log(result); // Expected output: "Promise 2 resolved"
} catch (error) {
console.error(error);
}
}
examplePromiseAny();
// Promise.race is settled as soon as any of the promises you feed it settle, whether they are fulfilled or rejected.
const { setTimeout } = require('timers/promises');
const promise1 = setTimeout(100, 'Promise 1 resolved');
const promise2 = setTimeout(200).then(() => { throw new Error('Promise 2 rejected'); });
const promise3 = setTimeout(300, 'Promise 3 resolved');
async function examplePromiseRace() {
try {
const result = await Promise.race([promise1, promise2, promise3]);
console.log(result); // Expected output: "Promise 1 resolved"
} catch (error) {
console.error(error);
}
}
examplePromiseRace();
// Promise.race is settled as soon as any of the promises you feed it settle, whether they are fulfilled or rejected.
const { setTimeout } = require('timers/promises');
const promise1 = setTimeout(100).then(() => { throw new Error('Promise 1 rejected'); });
const promise2 = setTimeout(200, 'Promise 2 resolved');
const promise3 = setTimeout(300, 'Promise 3 resolved');
async function examplePromiseRaceWithRejection() {
try {
const result = await Promise.race([promise1, promise2, promise3]);
console.log(result);
} catch (error) {
console.error(error.message); // Expected output: "Promise 1 rejected"
}
}
examplePromiseRaceWithRejection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment