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 Observable from 'rxjs/observable'; | |
// Action creator function | |
function fetchArticles(params) { | |
return { type: FETCH_ARTICLES, params }; | |
} | |
// Epic | |
const fetchArticlesEpic = action$ => { | |
return action$ | |
.ofType(FETCH_ARTICLES) | |
.switchMap(action => { | |
return Observable.concat( | |
Observable.of({ type: FETCH_ARTICLES_START }), | |
Observable.fromPromise( | |
ArticlesService.getArticles(action.params) | |
.then( | |
user => ({ type: FETCH_ARTICLES_SUCCESS, user }), | |
error => ({ type: FETCH_ARTICLES_FAILURE, error }) | |
) | |
) | |
); | |
}); | |
} | |
// Configuration | |
const epicMiddleware = createEpicMiddleware(fetchArticlesEpic); | |
const store = createStore( | |
reducer, | |
applyMiddleware(epicMiddleware) | |
); | |
// Actual usage | |
dispatch(fetchArticles(params)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment