【发布时间】:2019-09-09 11:10:51
【问题描述】:
我必须从多个 Endpoint 获取多个数据,然后合并到单个数据将发送到 reducer,然后我得到 Promise.all,获取多个 api 数据然后合并,我的问题是,如何检测哪个是失败,哪个是成功获取?那么哪个是成功仍然发送到reducer,一个失败发送失败消息
我使用 ReactJS、Redux 和 Redux Thunk,当所有端点都成功回调时,所有数据都已发送,但是当一个端点失败时,它只会抛出一个来自端点之一的错误
我做了什么:
const chartUrl = [
requestAPI('GET', 'endpoint/a', 2).then(res => res.json()),
requestAPI('GET', 'endpoint/b', 2).then(res => res.json()),
requestAPI('GET', 'endpoint/c', 2).then(res => res.json()),
requestAPI('GET', 'endpoint/d', 2).then(res => res.json()),
requestAPI('GET', 'endpoint/e', 2).then(res => res.json()), // this endpoint fail
requestAPI('GET', 'endpoint/f', 2).then(res => res.json()),
requestAPI('GET', 'endpoint/g', 2).then(res => res.json())
];
let chartObj = {
dataChart: {}
};
Promise.all(chartUrl)
.then(storeData => {
let mergedData = storeData.reduce((prev, cur) => {
prev[cur.message] = cur;
return prev;
}, {});
Object.assign(chartObj.dataChart, mergedData);
// Make detection which code=200 and success=true then dispatch to fetched_chart reducer, but when code != 200 and success=false then dispatch to fail_chart reducer
console.log(chartObj);
})
.catch(err => {
//when server has fail then dispatch to error_fetch_chart reducer
return err;
})
输出:
http://web.data.net/api/v1/endpoint/interaction 500 (Internal Server Error)
我希望这样的输出:
{
dataChart:
{
a: {
code: 200,
success: true
data: chartA
},
b: {
code: 200,
success: true
data: chartB
},
c: {
code: 200,
success: true
data: chartC
},
d: {
code: 200,
success: true
data: chartD
},
e: { //error callback
message: '500 (Internal Server Error)'
}
...
}
}
【问题讨论】:
-
Promise.all 如果有 any 个 promise 拒绝,则拒绝。我的第一个想法是在每个承诺上加上
.catch,这样一次失败就不会导致整个事情失败。 -
你可能想要Promise.allSettled,它在 chrome 中可用,但很容易被“填充”
标签: javascript reactjs redux promise