Resolve a promise once all internal concurrent promises have resolved or rejected

后端 未结 2 1756
青春惊慌失措
青春惊慌失措 2020-12-12 03:26

I am looking for something similar to Promise.all that will continue to resolve promises concurrently even in the event that one or more of the promises reject

2条回答
  •  不思量自难忘°
    2020-12-12 03:53

    I have succeeded in using Promise.all together with only ever resolving the fetchRequest promises

    That's basically the way to go. ES6 does not have a helper function like allSettled (Q) or settle (Bluebird 2.x) for this case, so we will need to use Promise.all similar to like you did. Bluebird even has a dedicated .reflect() utility for this.

    You would however not resolve them with undefined in the case of a rejection, but rather with some useful value that allows to identify the errors.

    function promiseRequests(requests) {
      return Promise.all(requests.map(request => {
        return fetch(request).then(res => {
          return {value:res};
        }, err => {
          return {reason:err};
        });
      }));
    }
    

提交回复
热议问题