Created
June 13, 2017 07:58
-
-
Save tricoder42/c14b62349577127dc9c2ffe827d5a4d2 to your computer and use it in GitHub Desktop.
2017/06/13 [Medium] redux-saga factories and decorators
This file contains 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 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