dva action一般是异步的,但是业务有时需要同步处理,总结三种处理方式如下:
-
async
处理函数
onSubmit = async (e) => {
let params = {
cityName: '',
cityId: '',
};
this.props.dispatch({ type: 'app/loadStart' });
try {
await this.props.dispatch({ type: 'editDivision/updateDivisionInfo', payload: params });
console.log('updateDivisionInfo执行成功后,才会打印');
this.props.dispatch({ type: 'app/loadSuccess' });
} catch (err) {
this.props.dispatch({ type: 'app/loadSuccess' });
}
}
- 利用
@@end
实现put
的同步执行
参考dva文档
先执行getCityRank,执行成功后,才会执行updateInfo
yield put({ type: 'getCityRank', payload: { cityId: 'all', employeeNo: 'all', teamId: 'all' } });
yield take('getCityRank/@@end');
yield put({ type: 'updateInfo', payload: { cityId: 'all', employeeNo: 'all', teamId: 'all' } });
yield take('updateInfo/@@end');
promise.then
先执行getCityRank,执行成功后,才会执行updateInfo
this.props.dispatch({ type: 'app/loadStart' });
this.props.dispatch({ type: 'reportForm/getCityRank', payload: form }).then(() => {
this.props.dispatch({ type: 'reportForm/updateInfo', payload: form })
this.props.dispatch({ type: 'app/loadSuccess' });
}).catch(() => {
this.props.dispatch({ type: 'app/loadSuccess' });
});