gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:36:24 25 4
gpt4 key购买 nike

我有一系列的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com