javascript - 如何有效地使用 bluebird .all 和 .reflect?

标签 javascript promise bluebird

我有一系列的 promise ,我需要等到所有 promise 都实现或被拒绝。 这是我正在做的

var = [promiseA,promiseB,promiseC]      
    Promise.all(promises.map(function(promise) {
     
       return promise.reflect();
    
    })).each(function(inspection) {
     
    if (inspection.isFulfilled()) {
    
    console.log("A promise in the array was fulfilled   with",inspection.value());
       
   } else {

      console.error("A promise in the array was 
       rejected with",  inspection.reason());
     
    }
       
 })

上面的代码打印了每个 promise 的实现或拒绝值。在我的例子中,这里的每个 promise 都返回一个成功或错误的 json。我需要使用 .then() 之类的函数获取所有成功的 json 值。

当我尝试使用 .then 获取值时

Promise.all(promises.map(function(promise) {
      
   return promise.reflect();
   
 })).then(data){
//_settledValue gives me the json value either success json or error json
   console.log('data[0]::::’+JSON.stringify(data[0]._settledValue));    
}.

我如何忽略错误 json 并只在此处获取成功 json? 谁能帮我解决这个问题?

最佳答案

按照其他人的建议使用 Array.filterBluebird.filter

Bluebird.all(promises.map(function(promise) {
          
  return promise.reflect();
       
}))
  .filter(function(promise) {return promise.isFulfilled();})
  // or .then(promises => promises.filter(/*...*/))
  .then(function (data) {
     // only successful ones are available here...
  });

关于javascript - 如何有效地使用 bluebird .all 和 .reflect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35755315/

相关文章:

javascript - 使用 promise 模式处理 office.js

javascript - Mocha 测试由 bluebird 驱动的 Node 样式回调

node.js - 使用 node-request 和 fs Promified 下载图像,在 Node.js 中没有管道

javascript - 按顺序运行列表中的函数,直到一个函数解析为止

javascript - 通过引用更改 DOM 元素

javascript - 如何在解析云代码中将文本文件附加到mandrill邮件?

javascript - HTML5 数据属性在 Safari 中丢失

javascript - Cordova SQLite 插件 promise javascript 链

javascript - JavaScript Promise 中的 "Resolve"函数

javascript - 尝试使用 AngularJS 将图像上传到 Cloudinary 时,Access-Control-Allow-Origin 不允许 Origin http ://localhost. com:8000