Created
December 16, 2018 15:20
-
-
Save productioncoder/3cb4620eb807b2f72f57b7f7f5fb631b to your computer and use it in GitHub Desktop.
Youtube in React: generic fetching function based on redux-saga
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
import {all, call, put} from 'redux-saga/effects'; | |
/* | |
... rest unchange for now | |
*/ | |
/* | |
* entity must have a success, request and failure method | |
* request is a function that returns a promise when called | |
* */ | |
export function* fetchEntity(request, entity, ...args) { | |
try { | |
const response = yield call(request); | |
// we directly return the result object and throw away the headers and the status text here | |
// if status and headers are needed, then instead of returning response.result, we have to return just response. | |
yield put(entity.success(response.result, ...args)); | |
} catch (error) { | |
yield put(entity.failure(error, ...args)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment