472,146 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,146 developers and data experts.

What is promise.all() and how to use it?

Promise is an special javascript object which use to do something in future. Like in real life when you commit to someone that's the commitment for the upcoming time. Same as promises has two possibilities as resolved and rejected.

Today we will learn about it's one of method named as Promise.all().

What is promise.all()?

The Promise.all() method is actually a promise that takes an array of promises(an iterate) as an input. It returns a single Promise that resolves when all of the promises passed as an iterate. Which have resolved or when the iterate contains no promises. In simple way, if any of the passed-in promises reject, the Promise.all() method asynchronously rejects the value of the promise that already rejected, whether or not the other promises have resolved.

Let's quick view in code:

Expand|Select|Wrap|Line Numbers
  1. ```
  2. Promise.all([p1, p2, p3])
  3.  .then(result) => {
  4.    console.log(result)
  5.  })
  6.  .catch(error => console.log(`Error in promises ${error}`))
  7. ```
As you can see, we parsed an array to Promise.all() method, Once all promises will be resolved then output will consoled.

Let's look on example :

Expand|Select|Wrap|Line Numbers
  1. var Promise1 = Promise.resolve(10);
  2. var Promise2 = 779;
  3. var Promise3 = new Promise((resolve, reject) => {
  4.   setTimeout(() => {
  5.     resolve("cooding");
  6.   }, 100);
  7. });
  8.  
  9. Promise.all([p1, p2, p3]).then(response => {
  10.   console.log(response); 
  11. });
  12. //output
  13. [10, 779, "cooding"]
In the event that the iterable contains non-promises values, they are ignored. Yet included in the returned promise array values.

How promises reject if any promises gone failed?

Let's call an function as doOperation

Expand|Select|Wrap|Line Numbers
  1. var doOperation = function(time) {
  2.   return new Promise(function (resolve, reject) {
  3.     setTimeout(function() {
  4.       if(time === 2000) {
  5.         reject("error at 2000");
  6.       }
  7.       resolve(time);
  8.     }, time);
  9.   });
  10. }
It will be reject if time is equal to 2000 , Promise.all should reject instantly

Expand|Select|Wrap|Line Numbers
  1. var promisesCall = [doOperation(2000), doOperation(1000), doOperation(3000)];
  2.  
  3. var promises = promiseAll(promisesCall);
  4.  
  5. promises.then(function(results) {  
  6.      console.log(results); 
  7. }).catch(function(error) {
  8.      console.log(error);});
It will return an erros at 2000 , Because promise value meet the condition which is equal to 2000.
Jul 14 '21 #1
0 2833

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Pierre Jelenc | last post: by
reply views Thread by Daisy | last post: by
5 posts views Thread by Mark Ingram | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.