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

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

问题描述

我正在寻找类似于 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.

工作但感觉不正确使用解决方法

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 pr omises

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

这基本上就是要走的路。 ES6没有辅助函数,如 allSettled (Q)或对于这种情况,结算 (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天全站免登陆