I'm using redux-saga in my project. I used redux-thunk before, so i can't setState ends of some async request. like
this.props.thunkAsync()
.then(){
this.setState({ '' });
}
Since thunk returns promise, i could use 'then'. But i can't do this with saga, because saga doesn't return promise. So i did it in componentWillReceiveProps by checking flag props (like REQUEST_SUCCESS,REQUEST_WAITING...) has been changed. I think it's not good way to solve this problem.
So.. My question is how can i do some works when async request ends in redux-saga!
You can do it this way. From component props you call the saga method and pass the function you want to execute after success or failure, like below
Every api call you make is processed as an async request but handled using a generator function in a saga.
So, After a successful api call, you can do the following possible things.
Pass a Callback after successfull api
}
Update Reducer State and get from props by mapStateToProps
Redux-saga
is slightly different fromthunk
since it is process manager, not simple middleware:thunk
performs reaction only on fired actions, butsaga
has its own "process" (Formally callback tick domain) and can manipulate with actions by effects.Usual way to perform async actions with
redux-saga
is splitting original actions toACTION_REQUEST
,ACTION_SUCCESS
andACTION_FAILURE
variants. Then reducer accepts only SUCCESS/FAILURE actions, and maybe REQUEST for optimistic updates.In that case, your
saga
process can be like followingKeep in mind that
yield
operation itself is not about promise waiting - it just delegates async waiting to saga process manager.