multiple `.then()`s on single angularjs promise — all use _original_ data

前端 未结 2 1533
醉酒成梦
醉酒成梦 2020-12-29 01:19

I\'m writing an angularjs app relying on promises, and though it\'s working, I wonder if I could do it more optimally.

At the beginning of the code, I\'m creating a

相关标签:
2条回答
  • 2020-12-29 01:49

    Turning my comment into an answer since it seems to clear up the issue:

    With your pattern, the two .then() calls on the same promise are going to get called one after another when the promise is resolved. The second .then() has only to do with the original promise and nothing to do with what happens on the first .then().

    These are not chained so the second .then() has no dependency upon what is returned from the first .then() and both will be passed the same data. They are just multiple watchers of the same promise kind of like two event handlers listening for the same event.

    The two .then() handlers on the same promise will be called in the order they were attached to the promise and will both be passed the same data.

    See these two answers:

    Is there a difference between promise.then.then vs promise.then; promise.then

    Understanding javascript promises; stacks and chaining

    for more info on chaining p.then(...).then(...) vs. branching p.then(...); p.then(...) with promises.

    0 讨论(0)
  • 2020-12-29 01:50

    You need parallel execution: $q.all()

    $q.all(
        function1,
        function2,
        function3
    ).then(function(responses) {
        console.log(responses);
    });
    
    0 讨论(0)
提交回复
热议问题