Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tricoder42
Created June 13, 2017 07:58
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 tricoder42/c14b62349577127dc9c2ffe827d5a4d2 to your computer and use it in GitHub Desktop.
Save tricoder42/c14b62349577127dc9c2ffe827d5a4d2 to your computer and use it in GitHub Desktop.
2017/06/13 [Medium] redux-saga factories and decorators
export const fetchSaga = (entity, api) => function* ({ payload }) {
try {
const data = yield call(api, payload)
yield put(entity.response(data))
}
catch(error) {
// When there's error.response, it's usually response from
// backend server with status 4xx or 5xx.
// No response mostly means there's a connection error, because
// we don't even get anything from server.
if (!error.response) {
// connection error
yield put(networkAction.networkDown())
yield fork(reloadEntity, entity, payload)
}
yield put(entity.response(error))
}
}
// reload entity once the network is back up
export function* reloadEntity (entity, payload) {
yield take(networkAction.networkUp)
yield put(entity.request(payload))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment