Skip to content

Instantly share code, notes, and snippets.

@oakfang
Created February 16, 2016 09:49
Show Gist options
  • Select an option

  • Save oakfang/0fc91609bcf480750620 to your computer and use it in GitHub Desktop.

Select an option

Save oakfang/0fc91609bcf480750620 to your computer and use it in GitHub Desktop.
Redux middleware for simple fetches
const asyncFetchMiddleware = store => next => action => {
let {type, request} = action;
if (type.endsWith('_ASYNC')) {
fetch(request)
.then(response => response.json())
.then(payload => store.dispatch({
type: type.replace(/ASYNC$/, 'SUCCESS'),
payload
}))
.catch(payload => store.dispatch({
type: type.replace(/ASYNC$/, 'FAILURE'),
payload
}))
}
return next(action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment