Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

🐰 🐱 🐻 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

Repository files navigation

build passnpm-versionlicensebower-license

redux-actions-promise

FSA-compliant promise middleware for Redux, supports referencing dispatcher/state in action.

Installation

Install the pkg with npm:

npm install redux-actions-promise --save

or yarn

yarn add redux-actions-promise

or bower

bower install redux-actions-promise

Usage

// 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);

FAQ

  1. why not useredux-thunk ?
  • I would like to usecreateAction forFSA.
  • 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        }    });};
  1. why not useredux-promise ?

Because it doesn't support referencing dispatcher or state in action. See#20.

LICENSE

MIT

About

🐰 🐱 🐻 FSA-compliant promise middleware for Redux, supports referencing dispatcher/state in action.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp