There are methods such as Q.reduce
and Q.all
that help smooth out the promise chain for a particular case of dissimilar promises collections. Of course, the general case:
const F = (x) => x; const a = F(1); const b = F(2); const c = F(a + b); const d = F(a + c); const e = F(b + c); console.log(e);
That is, a sequence of tasks on which each term depends on arbitrary previously defined members. Suppose F
is an asynchronous call:
const F = (x) => Q.delay(1000).return(x);
I cannot express this pattern in any way without creating an indented pyramid:
F(100).then(a => F(200).then(b => F(a+b).then(c => F(a+c).then(d => F(b+c).then(e => F(d+e).then(f => console.log(f) ) ) ) ) ) );
Note that using return values will not work:
F(100).then(a => F(200)) .then(b => F(a+b)) .then(c => F(a+c)) .then(d => F(b+c)) .then(e => F(d+e)) .then(f => console.log(f));
Since, for example, a
will not be in scope in the second line. What is the right way to deal with this situation?
source share