Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Last active June 25, 2022 09:02
Show Gist options
  • Save Calvin-Huang/53173f7835227f044dbbb163975d1b4f to your computer and use it in GitHub Desktop.
Save Calvin-Huang/53173f7835227f044dbbb163975d1b4f to your computer and use it in GitHub Desktop.
import { takeEvery } from 'redux-saga';
import { call, put } from 'redux-saga/effects';
// Watcher
function* watchFetchData() {
yield* takeEvery('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});
}
}
+export default function* rootSaga() {
+ yield all([
+ fork(watchFetchData),
+ ]);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment