Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Created December 16, 2018 15:20
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 productioncoder/3cb4620eb807b2f72f57b7f7f5fb631b to your computer and use it in GitHub Desktop.
Save productioncoder/3cb4620eb807b2f72f57b7f7f5fb631b to your computer and use it in GitHub Desktop.
Youtube in React: generic fetching function based on redux-saga
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