// 假设有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>
...
两种解决方案
1.async
2.promise.all/promise.allsettled