Javascript:无法同时获得 Promises.all 个回复
Javascript: Unable to get Promises.all responses together
我正在使用 React-Redux 操作来立即获得一些承诺响应。事实上,我需要 link 这些响应合并为一个响应。
我有一个解析 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,然后返回到视图组件中,我尝试将它们放在一起,但有时它不起作用,因为 resp1 道具在 resp2 道具之前获得 ComponentDidmount。
如何将它们组合成一个响应,然后将其传递给视图组件?
你的问题不是很清楚。如果您想在发送之前解析两个响应,那么 最简单的 方法是将解析链接到获取并在结果上使用 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
});
我正在使用 React-Redux 操作来立即获得一些承诺响应。事实上,我需要 link 这些响应合并为一个响应。 我有一个解析 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,然后返回到视图组件中,我尝试将它们放在一起,但有时它不起作用,因为 resp1 道具在 resp2 道具之前获得 ComponentDidmount。 如何将它们组合成一个响应,然后将其传递给视图组件?
你的问题不是很清楚。如果您想在发送之前解析两个响应,那么 最简单的 方法是将解析链接到获取并在结果上使用 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
});