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 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-thunk-promise

v2.0.1

Published

Thunk and FSA promise middleware for Redux

Downloads

928

Readme

Redux Thunk Promise

Build Status Coverage Status semantic-release npm npm

Thunk and FSA promise middleware for Redux.

Benefits

  • It lets you work with thunks, promises or both. You get to choose what to use, depending on what you need.
  • It lets you write sync & async action using the same syntax.
  • And it lets you take advantage of the best of both worlds:
    • The ability to dispatch other actions, like loading, and get information from the state (from thunks).
    • The simplicity of firing async calls and getting the result in the reducer, even if it's not successful (from promises).
  • If your thunk returns a promise, you'll get a unified result in your reducer, an FSA-compliant promise.

Installation

npm i -S redux-thunk-promise

Then, add this middleware in your Redux store:

import { createStore, applyMiddleware } from 'redux';
import thunkPromiseMiddleware from 'redux-thunk-promise';
import { reducers } from './domain';

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

See a real-life example here, you can run it locally by cloning the repo and running npm start.

Usage

Let's say you want to define two actions, one to fetch tasks from your server (async) and other to mark tasks as completed in the UI (sync). You can do this by using the following code in your Redux container:

...

const mapDispatchToProps = dispatch => ({
  fetchTasks: () => dispatch(actions.fetchTasks()), // async action
  completeTask: taskId => dispatch(actions.completeTask({ id: taskId })), // sync action
});

export default connect(mapStateToProps, mapDispatchToProps)(TodoList);

Note: you don't need to differentiate between async and sync actions in the container.

Then, you can write your async actions in different ways:

  1. FSA promise: This is best suited for basic scenarios when you only care about the action. redux-thunk-promise will take care of sending it to the reducer, following the Flux Standard Action pattern. This means that your reducer will receive either the data or the error in the payload (see an example here).

    Your only job is to create async actions this way:

    import { createAction } from 'redux-actions';
    import actionTypes from './action-types';
    import tasksService from './tasks-service';
    import { actions as uiActions } from '../ui';
    
    const fetchTasksFromService = () => tasksService.fetchTasks();
    
    export default {
      fetchTasks: createAction(actionTypes.FETCH_TASKS, fetchTasksFromService),
      completeTask: createAction(actionTypes.COMPLETE_TASK),
    };
  2. Thunk: in this case, you wrap the function into another function. With thunks, you can dispatch other actions, get information from the state or even get global arguments before/after the main action. For instance:

    import { createAction } from 'redux-actions';
    import actionTypes from './action-types';
    import tasksService from './tasks-service';
    import { actions as uiActions } from '../ui';
    
    const fetchTasksFromService = () => ({ dispatch, getState, ...extraArguments }) => {
      dispatch(uiActions.isLoading(true));
    
      // The result of the call will be sent to reducers as the payload of the FETCH_TASKS action.
      // If there was an error, action.error will be set to true
    
      return tasksService.fetchTasks()
        .then(() => actions.isLoading(false))
        .catch(() => actions.isLoading(false));
    };
    
    export default {
      fetchTasks: createAction(actionTypes.FETCH_TASKS, fetchTasksFromService),
      completeTask: createAction(actionTypes.COMPLETE_TASK),
    };

Note: No matter which approach you choose for your async actions, your reducers will receive an FSA-compliant action.

Using in combination with redux-actions

We recommend combining this library with redux-actions.