AngularJS: $q wait for all even when 1 rejected

后端 未结 8 1577
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 16:23

I\'ve been trying to wait for a couple of promises with Angular\'s $q but there seems to be no option to \'wait for all even when a promis is rejected\'. I\'ve created an exampl

8条回答
  •  不思量自难忘°
    2021-02-01 17:11

    I solved this same issue recently. This was the problem:

    • I had an array of promises to handle, promises
    • I wanted to get all the results, resolve or reject
    • I wanted the promises to run in parallel

    This was how I solved the problem:

    promises = promises.map(
        promise => promise.catch(() => null)
    );
    $q.all(promises, results => {
        // code to handle results
    });
    

    It's not a general fix, but it is simple and and easy to follow. Of course if any of your promises could resolve to null then you can't distinguish between that a rejection, but it works in many cases and you can always modify the catch function to work with the particular problem you're solving.

提交回复
热议问题