Skip to content

Instantly share code, notes, and snippets.

View jayphelps's full-sized avatar
💭
I may be slow to respond.

Jay Phelps jayphelps

💭
I may be slow to respond.
View GitHub Profile
@jayphelps
jayphelps / redux-thunk-examples.js
Created February 7, 2017 05:44 — forked from markerikson/redux-thunk-examples.js
Redux-Thunk examples
// The classic AJAX call - dispatch before the request, and after it comes back
function myThunkActionCreator(someValue) {
return (dispatch, getState) => {
dispatch({type : "REQUEST_STARTED"});
myAjaxLib.post("/someEndpoint", {data : someValue})
.then(response => dispatch({type : "REQUEST_SUCCEEDED", payload : response})
.catch(error => dispatch({type : "REQUEST_FAILED", error : error});
};
}
/**
* README FIRST::::::::::::::::::
*
* This isn't meant to necessarily be an example of "best practices"
* but rather to generally show how you might convert the redux-thunk examples
* here: https://gist.github.com/markerikson/ea4d0a6ce56ee479fe8b356e099f857e
*
* The redux-thunk examples used generic placeholder names, so it's possible
* I misunderstood the usecase/intent.
*
const oauthUserEpic = (action$) =>
action$
.ofType(OAUTH_USER)
.concatMap(({ token }) =>
get({
endpoint: 'user',
params: { access_token: token }
}) // assuming get() returns an Observable
.map((user) =>
addUser(