Skip to content

Instantly share code, notes, and snippets.

@rowlandekemezie
Last active March 23, 2019 12:34
Show Gist options
  • Save rowlandekemezie/2c1823f442ecca28125528c1af907e54 to your computer and use it in GitHub Desktop.
Save rowlandekemezie/2c1823f442ecca28125528c1af907e54 to your computer and use it in GitHub Desktop.
Saga for making calls to gitHubApi
// Here, we use ES6 destructuring assignment to extract payload
// from the action object passed to it.
function* loadUserDetails({ payload }) {
try {
const user = yield call(gitHubApi, payload);
// Yields effect to the reducer specifying action type
// and user details.
yield put({type: 'LOAD_USER_SUCCESS', user});
} catch (error) {
yield put({ type: 'LOAD_USER_FAILURE', error });
}
}
// Call loadUserDetails with action's payload each time
// LOAD_USER_REQUEST is dispatched
function* watchRequest() {
yield takeLatest('LOAD_USER_REQUEST', loadUserDetails);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment