Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise.all().then() resolve?

Using Node 4.x. When you have a Promise.all(promises).then() what is the proper way to resolve the data and pass it to the next .then()?

I want to do something like this:

Promise.all(promises).then(function(data){   // Do something with the data here }).then(function(data){   // Do more stuff here }); 

But I'm not sure how to get the data to the 2nd .then(). I can't use resolve(...) in the first .then(). I figured out I can do this:

return Promise.all(promises).then(function(data){   // Do something with the data here   return data; }).then(function(data){   // Do more stuff here }); 

But that doesn't seem like the proper way to do it... What is the right approach to this?

like image 949
Jake Wilson Avatar asked Oct 12 '15 04:10

Jake Wilson


People also ask

What is promise resolve () then?

resolve() The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.

What is the fulfilled value of promise all ()?

all() has what is called a "fast fail" implementation. It returns a master promise that will reject as soon as the first promise you passed it rejects or it will resolve when all the promises have resolved.

Does promise wait for resolve?

You can use the async/await syntax or call the . then() method on a promise to wait for it to resolve. Inside of functions marked with the async keyword, you can use await to wait for the promises to resolve before continuing to the next line of the function.


1 Answers

But that doesn't seem like the proper way to do it..

That is indeed the proper way to do it (or at least a proper way to do it). This is a key aspect of promises, they're a pipeline, and the data can be massaged by the various handlers in the pipeline.

Example:

const promises = [    new Promise(resolve => setTimeout(resolve, 0, 1)),    new Promise(resolve => setTimeout(resolve, 0, 2))  ];  Promise.all(promises)    .then(data => {      console.log("First handler", data);      return data.map(entry => entry * 10);    })    .then(data => {      console.log("Second handler", data);    });

(catch handler omitted for brevity. In production code, always either propagate the promise, or handle rejection.)

The output we see from that is:

 First handler [1,2] Second handler [10,20] 

...because the first handler gets the resolution of the two promises (1 and 2) as an array, and then creates a new array with each of those multiplied by 10 and returns it. The second handler gets what the first handler returned.

If the additional work you're doing is synchronous, you can also put it in the first handler:

Example:

const promises = [    new Promise(resolve => setTimeout(resolve, 0, 1)),    new Promise(resolve => setTimeout(resolve, 0, 2))  ];  Promise.all(promises)    .then(data => {      console.log("Initial data", data);      data = data.map(entry => entry * 10);      console.log("Updated data", data);      return data;    });

...but if it's asynchronous you won't want to do that as it ends up getting nested, and the nesting can quickly get out of hand.

like image 101
T.J. Crowder Avatar answered Sep 19 '22 23:09

T.J. Crowder