如何 使用 redux-thunk 同时发起2个异步action?

// 假设有2个数据接口
const getData1 = () => Promise.resolve('111');
const getData2 = () => Promise.resolve('222');

// action.js
const asyncAction1 = () => (dispatch) => {
   getData1().then((data) => {
     // 业务逻辑1...
     dispatch(data)
   })
}
const asyncAction2 =() => (dispatch) => {
   getData2().then((data) => {
     // 业务逻辑2...
     dispatch(data)
   })
}

// PageA.jsx
const handleGetAllData = () => {
  // 这里如何发起dispatch呢?再弄一个 asyncAction3把 1 和 2 包进去?然后 dispatch(asyncAction3)?
}
...
<Button onClick={handleGetAllData}>请求所有数据</Button>
...
阅读 2.2k
2 个回答

两种解决方案
1.async
2.promise.all/promise.allsettled

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题