Skip to content

Instantly share code, notes, and snippets.

@indigoviolet
Created December 23, 2017 03: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 indigoviolet/ce9104ab7ecdcdd965dc5ac90e52f83e to your computer and use it in GitHub Desktop.
Save indigoviolet/ce9104ab7ecdcdd965dc5ac90e52f83e to your computer and use it in GitHub Desktop.
fin blog embed
export const wrapObservableRequest = (type, observable, args = {}) =>
observable
.map(result => {
return { args, type, readyState: SUCCESS, data: result.data };
})
.catch(error => Observable.of({ args, type, readyState: FAILURE, error: getError(error) }))
.startWith({ args, type, readyState: REQUEST });
// Which can be used in an epic like this:
const postEpic = (actions$) => {
return actions$
.ofType(TRIGGER_FETCH)
.flatMap(action => {
return wrapObservableRequest(FETCH_POSTS, Observable.fromPromise(
fetch(`https://www.reddit.com/r/${action.subreddit}.json`)
.then(
response => response.json(),
error => console.log('An error occured.', error)
)
))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment