Forked from Calvin-Huang/redux-saga-worker-and-watcher.js
Last active
April 26, 2018 16:17
-
-
Save jasdeepkhalsa/074ef96e3ca60d29a03ed7b2b3db927f to your computer and use it in GitHub Desktop.
Redux Saga Example with Worker and Watcher
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 { call, put, takeLatest } from 'redux-saga/effects'; | |
/* | |
const EXAMPLE_DISPATCHED_ACTION = { | |
type: 'FETCH_REQUESTED', | |
payload: { url: 'https://api.github.com' } | |
} | |
*/ | |
// Watcher | |
export function* watchFetchData() { | |
yield takeLatest('FETCH_REQUESTED', fetchData); | |
} | |
// Worker | |
function* fetchData(action) { | |
try { | |
const data = yield call(fetch, action.payload.url); | |
yield put({type: 'FETCH_SUCCEEDED', data}); | |
} catch (error) { | |
yield put({type: 'FETCH_FAILED', error}); | |
} | |
} |
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 { all, call } from 'redux-saga/effects' | |
import { watchFetchData } from './fetchData' | |
export default function* rootSaga() { | |
yield all([ | |
call(watchFetchData), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment