-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to support return promise for dispatch(EFFECT_ACTION) #175
Comments
1.如果我要集成 异步关闭, https://ant.design/components/modal-cn/#components-modal-demo-async, onOk 必须接受一个promise 在then 里面 this.setState({ 在dva里面dispatch 一个action 后 ,
|
@nickzheng 尝试使用这个方法: 在组件里面使用 promise 包装 dispatch,例如: new Promise((resolve, reject) => {
dispatch({
type: 'users/create',
payload: {
values,
resolve,
reject,
},
});
})
.then( res => {
console.log(res);
})
.catch( err => {
console.log(err);
}) 而在 dva 的 effect 里面可以这样写: * create({ payload }, { put, call }) {
const { value, resolve, reject } = payload;
// 一些操作之后
if (success) {
resolve('done');
} else {
reject('error');
}
} @sorrycc 之前的想法是不需要在发起 dispatch 的地方包装一层 promise,而 dva 可以自带这个功能,默认的 effect 可以返回 resolve 和 reject。 |
https://github.com/dvajs/dva-core/blob/0fa6888/test/effects-test.js#L332-L365 ,内置支持 dispatch(EffectAction) 返回 Promise 。 |
@sorrycc 赞一个,问下这个功能是哪个版本加上去的?好像没看到changelog? |
It's supported in dva@2 . |
请问内置支持promise后, 在effect中如何定义resolve和reject? |
内置promise的意思是把上面变成下面么?
|
project const { dispatch } = this.props;
dispatch({
type: 'specification/fetch',
payload: applicationId,
}).then((data) => {
console.log(data)
}); model effects: {
* fetch({ payload }, { call, put, select}) {
const response = yield call(querySpecification, payload);
yield put({
type: 'list',
payload: response
});
return yield select(state => state.specification.list); //important
}
},
reducers: {
list(state, action) {
return {
...state,
list: action.payload
};
}
} |
@Youthink 像下面我这种情况怎么办 project
model
我想不管是后端返回成功还是有异常,都能执行到 |
Ref:
The text was updated successfully, but these errors were encountered: