Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oakley808
Created October 6, 2016 15:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oakley808/76dc872804037db0ec46fc7cb06eef91 to your computer and use it in GitHub Desktop.
Save oakley808/76dc872804037db0ec46fc7cb06eef91 to your computer and use it in GitHub Desktop.
A recipe for polling an API in redux-saga
export function* main() {
const { payload } = yield take(SOME_START_SIGNAL);
const watcherInstance = yield fork(updateResource, payload);
// cancel task instance on location change
yield take(LOCATION_CHANGE);
yield cancel(watcherInstance);
}
function* updateResource(id) {
const result = yield call(pollAPI, id);
yield put(taskSuccess(result));
}
function* pollAPI(id) {
while (true) {
try {
const result = yield call(fetchData, id);
return result;
} catch (error) {
yield put(taskFailure(error));
yield call(delay, 5000);
}
}
}
function fetchData(id) {
// return fetch(url, {id});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment