Skip to content

Instantly share code, notes, and snippets.

@nihaux
Last active February 18, 2017 02:00
Show Gist options
  • Save nihaux/5e40111ee77f212f9c2d05aef3ea09d6 to your computer and use it in GitHub Desktop.
Save nihaux/5e40111ee77f212f9c2d05aef3ea09d6 to your computer and use it in GitHub Desktop.
import { call, put, takeEvery } from 'redux-saga/effects'
// every time a FETCH_USER_INFO action is dipatched, the function fechtUserInfo will be called
function* watchFetchUserInfo() {
yield takeEvery(FETCH_USER_INFO, fetchUserInfo);
}
function* fetchUserInfo (action) {
try {
// tell redux-saga => call this functions and once done put them in pictureResponse, infoResponse
const [pictureResponse, infoResponse] = yield [
call(fetch, `https://graph.facebook.com/${action.payload.userID}/picture?type=large&redirect=false`),
call(fetch, `https://graph.facebook.com/v2.5/me?fields=email,name,friends&access_token=${action.payload.accessToken}`),
];
// here both call are resolved
// tell redux-saga => dipatch this actions in redux
yield put(receiveProfilePicture(pictureResponse.json().data.url));
yield put(receiveUserInformation(infoResponse.json());
// here both actions have been dispatched and runned through reducers
// you can do whatever you need, put to dispatch an action, call another func, etc
} catch (e) {
// here you can handle if either of the call failed
yield put({type: "USER_FETCH_FAILED", message: e.message});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment