What are the differences between call and put in redux-saga?

Technology CommunityCategory: ReduxWhat are the differences between call and put in redux-saga?
VietMX Staff asked 3 years ago

Both call and put are effects creators functions.

  • call function is used to create effect description, which instructs middleware to call the promise.
  • put function creates effect, in which instructs middleware to dispatch an action to the store.

Let’s take example of how these effects work for fetching particular user data

function* fetchUserSaga(action) {
  // `call` function accepts rest arguments, which will be passed to `api.fetchUser` function.
  // Instructing middleware to call promise, it resolved value will be assigned to `userData` variable
  const userData = yield call(api.fetchUser, action.userId);
  // Instructing middleware to dispatch corresponding action.
  yield put({
    type: 'FETCH_USER_SUCCESS',
    userData
  });
}