Created
October 6, 2016 15:47
-
-
Save oakley808/76dc872804037db0ec46fc7cb06eef91 to your computer and use it in GitHub Desktop.
A recipe for polling an API in redux-saga
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
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