一旦所有内部并发承诺都已解决或被拒绝,则解决承诺 [英] Resolve a promise once all internal concurrent promises have resolved or rejected

查看:14
本文介绍了一旦所有内部并发承诺都已解决或被拒绝,则解决承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找类似于 Promise.all 的东西,即使在一个或多个承诺拒绝或抛出错误的情况下,它也将继续同时解决承诺.每个请求不依赖于另一个请求.

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 or throw an error. Each request does not rely on another request.

接近我想要的 - 请看评论

function fetchRequest (request) {
  return new Promise(function (resolve, reject) {
    fetch(request)
    .then(function(response) {
      return response.text();      

    }).then(function (responseXML) {
      //Do something here. Maybe add data to dom
      resolve(responseXML);

    }).catch(function (err) {
      reject(new Error(err));
    }
}

function promiseRequests (requests) {
  var result = Promise.resolve();

  for (var i = 0; i < requests.length; i++) {
    result = fetchRequest(requests[i])
  }

  //This is wrong as it will resolve when the last promise in the requests array resolves
  // - not when all requests resolve
  resolve(result);
}

promiseRequests(['url1.com', 'url2.com']).then(function (data) {
  console.log('All requests finished');
  //optionally have data be an array of resolved and rejected promises
});

我成功地使用了 Promise.all 并且只解决了 fetchRequest 承诺,这导致了预期的结果(结果和 undefined 的数组)但我觉得这是错误的做事方式.它还使我无法使用抛出的错误.

I have succeeding in using Promise.all together with only ever resolving the fetchRequest promise and this results in the expected outcome (an array of results and undefined's) but I feel like this is the wrong way to do things. It also removes my ability to use thrown errors.

有效但感觉不正确使用resolve

function fetchRequest (request) {
  return new Promise(function (resolve, reject) {
    fetch(request)
    .then(function(response) {
      return response.text();      

    }).then(function (responseXML) {
      resolve(responseXML);

    }).catch(function (err) {
      resolve();
    }
}

Promise.all([fetchRequest('url1.com'), fetchRequest('url2.com')]).then(function (data) {
  console.log('All requests finished', data); //data could be ['resultXML', undefined]
});

请仅使用原生 es6 promise API 回答谢谢.

Please only native es6 promise API answers thanks.

推荐答案

我已经成功地使用了 Promise.all 并且只解决了 fetchRequest 承诺

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

这基本上就是要走的路.ES6 没有像 allSettled (Q) 或 settle (Bluebird 2.x) 对于这种情况,我们需要使用 Promise.all 类似于您所做的.Bluebird 甚至为此专门提供了 .reflect() 实用程序.

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.

然而,在拒绝的情况下,您不会使用 undefined 解决它们,而是使用一些允许识别错误的有用值.

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};
    });
  }));
}

这篇关于一旦所有内部并发承诺都已解决或被拒绝,则解决承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆