Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Forked from Calvin-Huang/redux-saga-worker-and-watcher.js
Last active April 26, 2018 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/074ef96e3ca60d29a03ed7b2b3db927f to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/074ef96e3ca60d29a03ed7b2b3db927f to your computer and use it in GitHub Desktop.
Redux Saga Example with Worker and Watcher
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});
}
}
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