0

I want to construct a promise chain as below. The arrows represent dependence: B needs the output of A; C needs the outputs of both A and B.

Since it is not simply a chain but is a net, I don't know how to do it in an elegant way. My idea is to let the output of B contain the output of A, and pass it all together to C through edge (B,C). I guess it's probably not the best way to do it because there should be some method by passing the result of A directly to C.

AngularJS promise chain

James Z
  • 12,209
  • 10
  • 24
  • 44
lys1030
  • 283
  • 1
  • 5
  • 17

1 Answers1

3

If you really want to provide C with the independent promise results, try something like this

var promises = { a: A() };
promises.b = promises.a.then(function(a) {
    return B(a);
});

Promise.all(promises).then(function(results) {
    C(results.a, results.b);
});

Otherwise, I see no problem with

A().then(function(a) {
    B(a).then(function(b) {
        C(a, b);
    });
});

but really, these are just two ways of writing the same thing.

Phil
  • 157,677
  • 23
  • 242
  • 245