Created
August 31, 2017 11:32
-
-
Save waliurjs/a7f990922ed1e8d65ec3f233752a84bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Redux-Saga | |
import axios from 'axios' | |
function* watchSaga(){ | |
yield takeEvery('fetch_user', fetchUser) // waiting for action (fetch_user) | |
} | |
function* fetchUser(action){ | |
try { | |
yield put({type:'fetch_user_ing'}) | |
const response = yield call(axios.get,'/api/users/1') | |
yield put({type:'fetch_user_done',user:response.data}) | |
} catch (error) { | |
yield put({type:'fetch_user_error',error}) | |
} | |
} | |
// Redux-Observable | |
import axios from 'axios' | |
const fetchUserEpic = action$ => | |
action$ | |
.ofType('fetch_user') | |
.flatMap(()=> | |
Observable.from(axios.get('/api/users/1')) // or use Observable.ajax | |
.map(response=>({type:'fetch_user_done', user:response.data})) | |
.catch(error => Observable.of({type:'fetch_user_error',error})) | |
.startWith({type:'fetch_user_ing'}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment