The Complete Guide to Promise Static Methods in Modern JavaScript

JavaScript is single threaded language. But think about a case where we call a network and still the network is connected what can we do, just wait and wait because it is single threaded so one work at a time. But dose this works in now a days, absolutely no. Another example be like when we make a call for read a file, till we complete to read that file we cannot perform another task, because it is a single threaded one task at a time. So, we use promises here and make task simultaneously. Once you make a promise then wait for future to happen this. The promises is like "Trust Me Bro".
States of Promises
Promises has three states
Pending
Fulfill or resolve
Rejected
Promise also have 2 parts in it
Resolve
Reject
We care about both resolve and reject in every promises.
Code
// Creating a promise
const promise = new Promise((res, rej) => {
setTimeout(() => {
res("chaicode");
}, 2000)
});
// Executing a promise
promise.then((value) => {
console.log("This is the start: ", value);
});
This is the start: chaicode
Static Methods in JavaScript
JavaScript also provides static methods on the Promise object for handling multiple promises at once.
Promise.all()
Think of it like you are making a cake and you send three of your friends to buy ingredients to make the cake. Now you have to wait till all your friends to come up with all the ingredients so that you can make the cake.
Similarly this all method takes a iterable of promises means one or more than one promises and fulfills when all promises in the iterable are fulfilled, reject immediately if any promise rejects. When all promises fulfills it returns an array with all fulfilled values.
const allPromise = Promise.all([
Promise.resolve("Code"),
Promise.resolve("JS"),
])
allPromise.then(console.log)
[ 'Code', 'JS' ]
Promise.any()
Let’s suppose you are getting a bug in your code and you just copy your code and error and paste it to LLMs to solve it. For example, you put your code to Gemini, ChatGPT, Claude and Grok. Now you are waiting for answers, after 4 of the LLMs gives answers you pick the answers one by one and check if it is working or not. At the point when you are get the correct answer you stop copy paste the other remaining answers because you get the answer and you don’t need the other response.
Similar to this case the we have any method where it takes one or more than one promises and return promise fulfilled when any one of the promise value fulfilled, with this first fulfilment value.
const allPromise = Promise.any([
Promise.resolve("JS"),
Promise.resolve("Code"),
Promise.reject("Error"),
])
allPromise.then(console.log)
JS
Promise.allSettled()
Suppose a teacher taking a test of 50 people and after the result there are some student who are passed(resolved) in the exam and some students are failed(rejected). For any one to failed in the exam does not reject the whole exam. At the end we got the result who got passed and who got failed in the exam.
Similarly this allSettled method takes one or more than one promises and returns a array of objects with all resolve and rejects values.
const allPromise3 = Promise.allSettled([
Promise.resolve("Code"),
Promise.resolve("JS"),
Promise.reject("Error"),
])
allPromise3.then(console.log)
[
{ status: 'fulfilled', value: 'Code' },
{ status: 'fulfilled', value: 'JS' },
{ status: 'rejected', reason: 'Error' }
]
Promise.race()
Imagine you are late for flight and you open Uber and request a ride , then you open Rapido and request a bike ride, also you stick your hands out for the taxi on the rode. Now for you it does not matter which ride you pick your aim is to reach the airport on time. A taxi sees you immediately and pick up you for the airport, now the Uber and Rapido does not matter.
Similarly this race method takes one or more than one promises and returns when the
const promise1 = new Promise((res, rej) => {
setTimeout(res, 200, "JS")
});
const promise2 = new Promise((res, rej) => {
setTimeout(res, 100, "Code")
})
Promise.race([promise1, promise2]).then((val) => {
console.log(val);
})
Code
Promise.resolve()
Imagine your friend ask you for a pen and you have one at your hand and you are not using it. So generally you give that pen to your friend insteed of searching in the bag for another pen.
Same way the resolve is a method which resolves a given value to a promise. If the value is a promise, that promise it returns, if it is thenable we use .then to fulfil the promises.
const promise = Promise.resolve("Ready");
console.log(promise);
promise.then((val) => {
console.log(val);
})
Promise { 'Ready' }
Ready
Promise.reject()
Imagine some stranger comes to you and ask you for ride your bike. The obvious answer would be no, because you don’t know the person.
Same way the reject is a method that returns a promise object that is rejected for a reason.
function res(val) {
console.log("This is resolved");
}
function rej(val) {
console.log("This is a error");
}
Promise.reject(new Error("but why?")).then(res, rej);
This is a error




