Created
September 15, 2016 20:05
-
-
Save KensoDev/2cc206881fec4968952a83d3f9e0b839 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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