Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access previous promise results in a .then() chain?

I have restructured my code to promises, and built a wonderful long flat promise chain, consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results. However the resolution values from the middle of the sequence are not in scope in the last callback, how do I access them?

function getExample() {     return promiseA(…).then(function(resultA) {         // Some processing         return promiseB(…);     }).then(function(resultB) {         // More processing         return // How do I gain access to resultA here?     }); } 
like image 639
Bergi Avatar asked Jan 31 '15 10:01

Bergi


People also ask

How do you return from promise then?

Return valuereturns a value, the promise returned by then gets resolved with the returned value as its value. doesn't return anything, the promise returned by then gets resolved with an undefined value. throws an error, the promise returned by then gets rejected with the thrown error as its value.

How do you use then catch in promise?

In summary: then : when a promise is successful, you can then use the resolved data. catch : when a promise fails, you catch the error, and do something with the error information. finally : when a promise settles (fails or passes), you can finally do something.

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.

How do you catch errors in promise chains?

There are two ways in which you can handle errors in your promise chain, either by passing an error handler to then block or using the catch operator.


1 Answers

Break the chain

When you need to access the intermediate values in your chain, you should split your chain apart in those single pieces that you need. Instead of attaching one callback and somehow trying to use its parameter multiple times, attach multiple callbacks to the same promise - wherever you need the result value. Don't forget, a promise just represents (proxies) a future value! Next to deriving one promise from the other in a linear chain, use the promise combinators that are given to you by your library to build the result value.

This will result in a very straightforward control flow, clear composition of functionalities and therefore easy modularisation.

function getExample() {     var a = promiseA(…);     var b = a.then(function(resultA) {         // some processing         return promiseB(…);     });     return Promise.all([a, b]).then(function([resultA, resultB]) {         // more processing         return // something using both resultA and resultB     }); } 

Instead of the parameter destructuring in the callback after Promise.all that only became avail­able with ES6, in ES5 the then call would be replaced by a nifty helper method that was provided by many promise libraries (Q, Bluebird, when, …): .spread(function(resultA, resultB) { ….

Bluebird also features a dedicated join function to replace that Promise.all+spread combination with a simpler (and more efficient) construct:

… return Promise.join(a, b, function(resultA, resultB) { … }); 
like image 174
Bergi Avatar answered Sep 21 '22 11:09

Bergi