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?
});
}
Another answer, using
babel-node
version <6Using
async - await
npm install -g babel@5.6.14
example.js:
Then, run
babel-node example.js
and voila!Explicit pass-through
Similar to nesting the callbacks, this technique relies on closures. Yet, the chain stays flat - instead of passing only the latest result, some state object is passed for every step. These state objects accumulate the results of the previous actions, handing down all values that will be needed later again plus the result of the current task.
Here, that little arrow
b => [resultA, b]
is the function that closes overresultA
, and passes an array of both results to the next step. Which uses parameter destructuring syntax to break it up in single variables again.Before destructuring became available with ES6, a nifty helper method called
.spread()
was provided by many promise libraries (Q, Bluebird, when, …). It takes a function with multiple parameters - one for each array element - to be used as.spread(function(resultA, resultB) { …
.Of course, that closure needed here can be further simplified by some helper functions, e.g.
Alternatively, you can employ
Promise.all
to produce the promise for the array:And you might not only use arrays, but arbitrarily complex objects. For example, with
_.extend
orObject.assign
in a different helper function:While this pattern guarantees a flat chain and explicit state objects can improve clarity, it will become tedious for a long chain. Especially when you need the state only sporadically, you still have to pass it through every step. With this fixed interface, the single callbacks in the chain are rather tightly coupled and inflexible to change. It makes factoring out single steps harder, and callbacks cannot be supplied directly from other modules - they always need to be wrapped in boilerplate code that cares about the state. Abstract helper functions like the above can ease the pain a bit, but it will always be present.
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.
Instead of the parameter destructuring in the callback after
Promise.all
that only became available with ES6, in ES5 thethen
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 thatPromise.all
+spread
combination with a simpler (and more efficient) construct: