I'm kind of a JS noob, but so far I really like the ES6 / React / Immutable capability to do functional programming and FRP, and in particular, the promise api. I particularly like the pattern of chaining .then
's, eg, somePromiseGenerator().then(...).then(...).catch(...)
. This is perfect when the flow of asynchronous calls is perfectly linear. However, Often I want to pass results from the top of this chain to the end.
The sub-optimal pattern I've been using looks like:
somePromiseGenrator()
.then(r => {
const inter = makeSomeIntermediateResults(r);
const newPromise = makeNewPromise(r);
return Promise.all([newPromise, Promise.resolve(inter)]);
})
.then(r => {
handleR0andR1(r[0], r[1]);
})
this happens, for instance, when i get data from ElasticSearch and want to supplement it with stuff that's in SQL or Neo4J or call out to a secondary API.
Using Promise.all
and especially Promise.resolve
seems like a waste of effort. Is there a better way to do this work?