What are different methods of JavaScript promises?

Promise methods:

There are six methods of JavaScript Promise class. Those methods are:

  • Promise.all
  • Promise.allSettled
  • Promise.race
  • Promise.any
  • Promise.reject
  • Promise.resolve

Among all the promise methods Promise.all method is the most used one.

Promise.all

When we want many promises to execute parallelly and get the result after all the promises are successfully completed. In this kind of use cases Promise.all is ideal to use.

For example, we want to fetch data from external URLs and combine the result at the end. In this case we can use Promise.all.

Example Promise.all

    Promise.all([
        new Promise((resolve, reject) => resolve(1)),
        new Promise((resolve, reject) => resolve(2)),
        new Promise((resolve, reject) => resolve(3))
    ]).then((combinedResult) => {
        console.log(combinedResult) // Orders are maintained too
    }).catch((e) => {
        console.log(e)
    });
Share