Sitemap

Member-only story

Javascript: Promise Static Methods

6. Deep dive into Promise Static Methods

Press enter or click to view image in full size
Promise Static Methods
Promise Static Methods in Javascript

In this article, we are talking about Static Methods of Promise and itโ€™s implementation. Promise Static Methods are either fullfill, reject or settle(fullfill or reject) .

1. Promise.all

This returned promise fulfills when all of the inputโ€™s promises fulfill (including when an empty iterable is passed)

Output: An Array
Success: When, All of the promises fulfill

const p1 = Promise.resolve(3);
const p2 = 42;
const p3 = new Promise((resolve) => setTimeout(() => resolve('foo')));
const p4 = new Promise((r, reject) => setTimeout(() => reject('Rejected')));
var allPormiseResolved = Promise.all([p1, p2, p3]);

allPormiseResolved.then((values) => {
console.log(values); // Expected output: Array [3, 42, "foo"]
});
var onePromiseFailed = Promise.all([p1, p2, p4]);

onePromiseFailed.catch((values) => {
console.log(values); // Expected output: Rejected
});

2. Promise.allSettled

This returned promise fulfills when all of the inputโ€™s promises settle(Either fail or success).

Output: An Array
Success: All of the promises settle

Written by โœ… open src ๐Ÿš€ ๐Ÿš€

1. Open source contributor, Architect at Jio Platform Ltd. and The Author of fe-pilot. https://github.com/opensrc0/fe-pilot/