Search This Blog

2023/04/27

Promise.all ,Promise.any,Promise.race & Promise.allSettled in Node.js

 Promise.any:


Promise.any() is a method introduced in ES2021 (also known as ES12) that
takes an iterable
of Promises and returns a new Promise that is fulfilled
when any of the Promises in the iterable is fulfilled.

The resulting Promise is fulfilled with the value of the first Promise in
the iterable that
was fulfilled. If all of the Promises in the iterable
are rejected, then the resulting Promise is rejected with an AggregateError
that contains an
array of rejection reasons from all the Promises.

const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 300);
});

const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('bar'));
}, 200);
});

const promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('baz');
}, 100);
});

Promise.any([promise1, promise2, promise3])
.then((value) => {
console.log(value); // Output: 'foo'
})
.catch((error) => {
console.log(error); // Output: AggregateError: All promises were rejected
});

Promise.all:

Promise.all() is a method introduced in ES6 (ECMAScript 2015) that takes an
iterable of Promises
and returns a new Promise that is fulfilled with an array of values when all
of the Promises
in the iterable are fulfilled.

If any of the Promises in the iterable are rejected, then the resulting
Promise is rejected
with the reason of the first Promise in the iterable that was rejected.

const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(4);
}, 2000);
});
const promise3 = Promise.reject(new Error('Rejected'));

Promise.all([promise1, promise2])
.then((values) => {
console.log(values); // Output: [3, 4]
})
.catch((error) => {
console.log(error); // This will not be called
});

Promise.all([promise1, promise2, promise3])
.then((values) => {
console.log(values); // This will not be called
})
.catch((error) => {
console.log(error); // Output: Error: Rejected
});

Promise.race:

Promise.race() is a method introduced in ES6 (ECMAScript 2015) that takes
an iterable of Promises and
returns a new Promise that is fulfilled or rejected as soon as any of the
Promises in the iterable
are fulfilled or rejected.

Unlike Promise.all(), Promise.race() does not wait for all the Promises in
the iterable to complete.
Instead, it returns as soon as the first Promise in the iterable is
fulfilled or rejected.

const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 300);
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('bar'));
}, 200);
});
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('baz');
}, 100);
});
Promise.race([promise1, promise2, promise3])
.then((value) => {
console.log(value); // Output: 'baz'
})
.catch((error) => {
console.log(error); // Output: Error: bar
});

Promise.allSettled:
Promise.allSettled() is a method introduced in ES2020 (ECMAScript 2020)
that takes an iterable of Promises and
returns a new Promise that is fulfilled with an array of objects when
all of the Promises in the
iterable have settled (either fulfilled or rejected).


Each object in the array represents the outcome of each Promise in the
iterable, and has the following properties:

status: either "fulfilled" or "rejected".
value: the fulfillment value of the Promise if status is "fulfilled".
reason: the rejection reason of the Promise if status is "rejected".

const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(4);
}, 2000);
});
const promise3 = Promise.reject(new Error('Rejected'));

Promise.allSettled([promise1, promise2, promise3])
.then((results) => {
console.log(results);
// Output:
// [
// { status: 'fulfilled', value: 3 },
// { status: 'fulfilled', value: 4 },
// { status: 'rejected', reason: Error: Rejected }
// ]
});

Promise.race vs Promise.any:
Promise.race() and Promise.any() are both methods for dealing with Promises
in JavaScript,
but they have some differences in behavior.

Promise.race() takes an iterable of Promises and returns a new Promise that
is
fulfilled or rejected as soon as any of the Promises in the iterable are
fulfilled or
rejected. This means that if any of the Promises in the iterable resolve
or reject first,
the resulting Promise will also resolve or reject with the same value or
reason.

Promise.any() also takes an iterable of Promises and returns a new Promise
that is
fulfilled as soon as any of the Promises in the iterable are fulfilled.
However, if all of the Promises in the iterable are rejected, the resulting
Promise
will be rejected with an AggregateError that contains an array of rejection
reasons from all of the Promises.


No comments:

Post a Comment