473,426 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,426 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 3085

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

Similar topics

9
by: Eunice Santorini | last post by:
Every once in a while (especially when I visit suspect web sites :), every single window on my Microsoft Windows XP & Microsoft Windows 2000 operating systems goes haywire. All the windows...
7
by: Pierre Jelenc | last post by:
I've been asked to do a favor to a friend and fix the site at http://www.cwrpartners.com/ I'm totally baffled by the amount of rubbish in the header section; is that the kind of coding that...
0
by: Ken Varn | last post by:
Can someone provide a list of all of the command line arguments for vs7jit.exe? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. EmailID = varnk
0
by: Daisy | last post by:
I've dealt with inheriting classes and overriding methods etc., but I keep seeing loads of examples that override WndProc, such as http://www.codeproject.com/cs/miscctrl/treeview.asp What's...
1
by: foreman | last post by:
Hi there, Hello everybody. I am a newbie to dot net framework class lib. I am confused about those classes such as all of the stream classes and those XXXReader XXXWriter. In fact, I have tried...
5
by: Mark Ingram | last post by:
Right, ive got the following array with which to setup a color matrix. bm = 1; bm = 0; bm = 0; bm = 0; bm = 0; bm = 0; bm = 1; bm = 0; bm = 0; bm = 0; bm = 0; bm = 0; bm = 1; bm = 0; bm = 0; bm...
1
by: rsaikamesh | last post by:
Hi Friends, This need some advice from you. I was working on ASP.NET with c# programming 7 month back. Then i was asked to work in c++ with Linux. I have been just creating User Interfaces for...
3
by: bowtie | last post by:
if a code is written like below what does it mean: project1.openform form1 ,,,, i'm mainly concerned about the commas wat do they imply and what can be put after these commas
2
by: kchavir | last post by:
What are all the possible values that can be returned based on the following statements using the modulus operator ? a) intValue % 2 b) intValue % 3 c) intValue % 100
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.