Skip to content

Instantly share code, notes, and snippets.

@KensoDev
Created September 15, 2016 20:05
Show Gist options
  • Select an option

  • Save KensoDev/2cc206881fec4968952a83d3f9e0b839 to your computer and use it in GitHub Desktop.

Select an option

Save KensoDev/2cc206881fec4968952a83d3f9e0b839 to your computer and use it in GitHub Desktop.
import { takeEvery, takeLatest } from 'redux-saga'
import { call, put } from 'redux-saga/effects'
import Api from '...'
// worker Saga: will be fired on USER_FETCH_REQUESTED actions
function* fetchUser(action) {
try {
const user = yield call(Api.fetchUser, action.payload.userId);
yield put({type: "USER_FETCH_SUCCEEDED", user: user});
} catch (e) {
yield put({type: "USER_FETCH_FAILED", message: e.message});
}
}
/*
Starts fetchUser on each dispatched `USER_FETCH_REQUESTED` action.
Allows concurrent fetches of user.
*/
function* mySaga() {
yield* takeEvery("USER_FETCH_REQUESTED", fetchUser);
}
/*
Alternatively you may use takeLatest.
Does not allow concurrent fetches of user. If "USER_FETCH_REQUESTED" gets
dispatched while a fetch is already pending, that pending fetch is cancelled
and only the latest one will be run.
*/
function* mySaga() {
yield* takeLatest("USER_FETCH_REQUESTED", fetchUser);
}
export default mySaga;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment