npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

redux-await-action

v1.0.2

Published

Wait for redux actions in react components

Downloads

311

Readme

Await Redux Actions

A redux extension to wait for actions in react components Requires the installation of react (>= 16.3) and redux (>= 4)

If using redux, sometimes there is a need to wait with specific logic until an action is dispatched. This is especially needed when decoupling ajax requests from react components with redux-observable or redux-thunk into asynchronous actions.

Installation

npm i --save redux-await-action

Usage

Setting up the middleware

You need to create an instance of the event emitter and apply the middleware to your redux store.

import { createAwaitMiddleware, StoreAwaitEventEmitter } from 'redux-await-action';

const awaitEmitter = new StoreAwaitEventEmitter();
const storeAwaitMiddleware = createAwaitMiddleware(awaitEmitter);

const store = createReduxStore(
  rootReducer,
  applyMiddleware(storeAwaitMiddleware)
);

Register the Provider

To be able to wait for actions inside the react components, you need to add the StoreAwaitProvider in your JSX. It should be placed somewhere near your redux store provider.

import { Provider } from 'redux';
import { StoreAwaitProvider } from 'redux-await-action';

return (
    <Provider store={store}>
        <StoreAwaitProvider emitter={awaitEmitter}>
            {props.children}
        </StoreAwaitProvider>
    </Provider>
);

Wait for actions somewhere in your react components using the useAwaitAction() hook

import React from 'react';
import { connect, DispatchProp } from 'redux';
import { useAwaitAction } from 'redux-await-action';

export const myCoponent = connect()((props: DispatchProp) => {
    const awaitAction = useAwaitAction();

    const handleLoadData = () => {
        /**
         * This statement creates a promise that resolves when an action with type 'LOAD_DATA_SUCCEEDED' is dispatched.
         * If 'LOAD_DATA_FAILED' is dispatched earlier, the promise will reject
         */
        awaitAction('LOAD_DATA_SUCCEEDED', 'LOAD_DATA_FAILED').then(() => {
            window.location.href = 'https://example.org/data-loaded';
        });
        
        // This dispatches a redux action (is redux functionality).
        // The action should be async (using something like redux-observable) and dispatch 'LOAD_DATA_SUCCEEDED' when the data is successfully loaded.
        dispatch({ type: 'LOAD_DATA_START' });
    };
    
    return (
        <button onClick={handleLoadData}>
            Click Me!
        </button>
    );
});

Wait for actions somewhere in your react components using the withAwaitAction() HOC

Class based components can't use React hooks. So there needs to be an other way to inject the awaitAction method.

import React, { Component } from 'react';
import { connect, DispatchProp } from 'redux';
import { withAwaitAction, WithAwaitAction } from 'redux-await-action';

class MyCoponent extends Component<DispatchProp & WithAwaitAction> {
    private handleLoadData = () => {
        /**
         * This statement creates a promise that resolves when an action with type 'LOAD_DATA_SUCCEEDED' is dispatched.
         * If 'LOAD_DATA_FAILED' is dispatched earlier, the promise will reject
         */
        props.storeAwait('LOAD_DATA_SUCCEEDED', 'LOAD_DATA_FAILED').then(() => {
            window.location.href = 'https://example.org/data-loaded';
        });
        
        // This dispatches a redux action (is redux functionality).
        // The action should be async (using something like redux-observable) and dispatch 'LOAD_DATA_SUCCEEDED' when the data is successfully loaded.
        dispatch({ type: 'LOAD_DATA_START' });
    };
    
    public render() {
        return (
            <button onClick={handleLoadData}>
                Click Me!
            </button>
        );
    }
}

export default withAwaitAction(connect()(MyComponent));

The await method

The await method that is created from the hook or passed from the HOC takes 1 - 2 arguments. The first argument is the type of the action that needs to be dispatched in the happy case - so when the returne promise should resolve. The second argument ts the type of the action that needs to be dispatched when the returned promise should reject. Both arguments can either be a string (one action type) or an array containing multiple action types. If one of the arguments is an array, the promise will resolve or reject at the first action that contains one of the passed types. The method returns a promise. The returned promise will be resolved when one of the action types of the first parameter is dispatched. The promise resolves whith the dispatched action. So the dispatched action that made the promise resolve is passed to the then callback. The returned promise will be rejected when one of the action types of the second parameter is dispatched. The promise rejects the dispatched action. So the dispatched action that made the promise reject is passed to the catch callback.

Useful links