Created
February 16, 2016 09:49
-
-
Save oakfang/0fc91609bcf480750620 to your computer and use it in GitHub Desktop.
Redux middleware for simple fetches
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
| 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