redux-async-await-action-middleware
Redux middleware to enable async/await action creators
Motivation
Use async/await to make Redux as elegant to use with async flows as it is with synchronous, without the learning/complexity overhead of redux-saga or redux-observable etc.
Usage
Include the middleware
;; const store = ;
Basic async action creator
{ try const response = await ; const data = await response; // Action can be any kind, not just FSA return type: 'LOAD_DATA' payload: data catch error return type: 'LOAD_DATA' payload: error isError: true } ... ;
A more complete async action creator
; const LOAD_DATA = 'LOAD_DATA';const NO_DATA_AVAILABLE = 'NO_DATA_AVAILABLE';const AUTH_FAILED = 'AUTH_FAILED'; const errorHandlers = 404: 403: { try const response = await ; if responsestatusCode !== 200 const handler = errorHandlersresponsestatusCode; if handler return ; throw `Unexpected Response: :`; const data = await response; return data; catch error return error; } ... ;
Use with an in-progress action
; ; const LOAD_DATA = 'LOAD_DATA';const LOAD_DATA_IN_PROGRESS = 'LOAD_DATA_IN_PROGRESS'; const dispatch = storedispatch; { try ; const response = await ; const data = await response; return data; catch error return error; } ... ;
Cancel an async action
; const LOAD_DATA = 'LOAD_DATA';const CANCEL_LOAD_DATA = 'CANCEL_LOAD_DATA'; { try const response = await ; const data = await response; return data; catch error return error; } ... ; ... ;
Install
npm i redux-async-await-action-middleware
Test
npm inpm test