1.0.1 • Published 4 years ago

redux-controlled-promise v1.0.1

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

redux-controlled-promise

redux middleware to controll dispatcch order use promise

npm install redux-controlled-promise

HOW TO USE

  • install package
npm install redux-controlled-promise
  • add middleware
import { createStore, applyMiddleware } from 'redux';
import reduxControlledPromise from 'redux-controlled-promise';
import rootReducer from './reducers';

const store = createStore(rootReducer, applyMiddleware(reduxControlledPromise));

USE IN ACTION

  • control sequence

    import { createStore, applyMiddleware } from 'redux';
    import reduxControlledPromise from 'redux-controlled-promise';
    import rootReducer from './reducers';
    
    const store = createStore(rootReducer, applyMiddleware(reduxControlledPromise));
    
    // If we want to control the order of dispatch, We can follow the example below to program,The order of the following
    // example dispatch is LOAD_A_CONFIG => LOAD_B_CONFIG & LOAD_C_CONFIG => LOAD_D_CONFIG
    store.dispatch([
        {
            type: 'LOAD_A_CONFIG',
            config: [],
        },
        [
            {
                type: 'LOAD_B_CONFIG',
                config: [],
            },
            {
                type: 'LOAD_C_CONFIG',
                config: [],
            },
        ],
        {
            type: 'LOAD_D_CONFIG',
            config: [],
        },
    ]);
    
    // The return value of middleware is promise, So we can execute some callback functions after the dispatch
    store.dispatch([
        {
            type: 'LOAD_A_CONFIG',
            config: [],
        },
        {
            type: 'LOAD_B_CONFIG',
            config: [],
        },
    ]).then(res) => {
        // ...config loaded
    };
  • You can customize the return value that can't find the action type

    const errorAlert = () => ({
        // ... error dialog
    });
    const store = createStore(
        reducer,
        applyMiddleware(reduxControlledPromise.withErrorArgument(errorAlert)),
    );