$q promise with Underscore _each

后端 未结 3 1389
忘了有多久
忘了有多久 2020-12-07 02:30

So I have a method in a angularjs server that is calling a method that returns a promise for each method in a array. I am using underscore _each to loop through the array. I

3条回答
  •  星月不相逢
    2020-12-07 03:14

    You want to use $q.all, which takes an array of promises. So use map instead of each, and pass the result to $q.all(), which gives you a promise that waits for all of them. You don't even need that stuff array which is manually filled, but just can use the resolution value of that new promise.

    function processCoolStuff(coolStuffs) {
        return $q.all(_.map(coolStuffs, makeStuffCooler));
    }
    processCoolStuff(…).then(showAllMyCoolStuff);
    

提交回复
热议问题