This repository was archived by the owner on Jan 14, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork0
🐰 🐱 🐻 FSA-compliant promise middleware for Redux, supports referencing dispatcher/state in action.
License
NotificationsYou must be signed in to change notification settings
dwqs/redux-actions-promise
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
FSA-compliant promise middleware for Redux, supports referencing dispatcher/state in action.
Install the pkg with npm:
npm install redux-actions-promise --saveor yarn
yarn add redux-actions-promiseor bower
bower install redux-actions-promise// store.js// create storeimport ReduxActionsPromise from 'redux-actions-promise';import { createStore, applyMiddleware } from 'redux';// Note: this API requires redux@>=3.1.0export default createStore(rootReducer, applyMiddleware(ReduxActionsPromise));// actions.jsimport {createAction} from 'redux-actions';// async actionexport let addToDo = createAction(CONSTANT.ADD_TODO, (val) => async (dispatch, getState) => { console.log('prev state', getState()) let v = await Promise.resolve('todo: ' + val); return v;});// sync actionexport let deleteToDo = createAction(CONSTANT.DELETE_TODO);- why not useredux-thunk ?
- I would like to use
createActionforFSA. - I don't want to handle promise error in action by myself.
Inredux-thunk:
addToDo = (val) => async (dispatch, getState) => { let v = await Promise.reject('error'); // can't output in console console.log('addToDo'); dispatch({ type: "ADD_TODO", payload: { val: v } })};If promise is rejected, the programme will be abort. So if you don't want this happen, you should handle it by yourself:
addToDo = (val) => async (dispatch, getState) => { try{ let v = await Promise.reject('error'); // can't output in console console.log('addToDo'); dispatch({ type: "ADD_TODO", payload: { val: v } }) } catch(e) { dispatch({ type: "ADD_TODO_ERROR", payload: e, error: true }) }};Useasync-await-error-handling maybe simple the code:
import awaitTo from 'async-await-error-handling';//...addToDo = (val) => async (dispatch, getState) => { const [err, data] = await awaitTo(Promise.reject('error')); if(err){ dispatch({ type: "ADD_TODO_ERROR", payload: e, error: true }); return; } dispatch({ type: "ADD_TODO", payload: { val: data } });};- why not useredux-promise ?
Because it doesn't support referencing dispatcher or state in action. See#20.
MIT
About
🐰 🐱 🐻 FSA-compliant promise middleware for Redux, supports referencing dispatcher/state in action.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published