Reputation: 389
I want to refresh a list of items after I made some changes they are different endpoints to hit, and they need to be synchronous (I want to call the second function after getting the first function response).
After getting the response from inviteUsers()
action, I would like to call getAllUsers()
action to get the updated list.
My question is, what is the best practice to handle those sequentially calls? Should I add another saga
to listen for the INVITE_USERS_SUCCESS
and then call the second action, or should I call the getAllUsers()
action inside the inviteUsers()
action like this?:
// first action
function* inviteUsers(args) {
try {
yield call(() => API.post('/users/invite', args));
yield put({
type: CustomConstants.INVITE_USERS_SUCCESS
});
// call second action
yield call(getAllInvites);
} catch (e) {
yield put({
type: CustomConstants.INVITE_USERS_FAILURE,
error: e,
});
}
}
// second action (after first action response)
function* getAllInvites() {
try {
yield call(() => API.get('/users/all-invites'));
yield put({
type: CustomConstants.LIST_PENDING_INVITES_SUCCESS
});
} catch (e) {
yield put({
type: CustomConstants.LIST_PENDING_INVITES_FAILURE,
error: e,
});
}
}
Upvotes: 0
Views: 3884
Reputation: 3709
Should I add another saga to listen for the INVITE_USERS_SUCCESS and then call the second action
Yes, you should. You get the best of Redux-saga when you use only actions to trigger them and, more in general, to make them communicate (it's event-driven development).
This brings you to write more code but also to develop independent sagas.
or should I call the getAllUsers() action inside the inviteUsers() action
I don't recommend you to do that because your inviteUsers
saga won't be decoupled from the getAllUsers
one (because they are part of a bigger flow) and you won't be able to use them separately when you need a different flow.
Let me know if you need some more help 😉
Upvotes: 3