我正在使用 React-Redux 操作来立即获得一些 promise 响应。事实上,我需要将这些响应链接到一个组合响应中。 我有一个解析 JSON 响应的函数。
例如:
Promise.all([
fetch('some_backend_resource'),
fetch('another_backend_resource')])
.then(([response1, response2]) => {
parseJSON(response1).then(resp1 => {
// here I dispatch to another function the value of resp1
});
parseJSON(response2).then(resp2 => {
// here I dispatch to another function the value of resp2
});
})
然后,我将 resp1 和 resp2 值传递给各自的 reducer ,然后返回 View 组件,我尝试将它们放在一起,但有时它不起作用,因为 resp1 属性在 resp2 属性之前获取 ComponentDidmount 。 如何将它们组合成一个响应,然后将其传递给 View 组件?
最佳答案
你的问题不完全清楚。如果您想在分派(dispatch)之前解析两个响应,那么最简单的方法是将解析链接到获取并在结果上使用 Promise.all :
Promise.all([
fetch('some_backend_resource').then(r => parseJSON(r)),
fetch('another_backend_resource').then(r => parseJSON(r))])
.then(([resp1, resp2]) => {
// dispatch resp1 & resp2
});
关于Javascript:无法获得 Promises.all 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44551001/