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-promise-resolver

v1.0.5

Published

A simple redux middleware for dispatching async promises

Downloads

21

Readme

redux-promise-resolver

A simple middleware for dispatching async redux actions. This is very similar to redux-promise-thunk except it is a middleware function and does not require redux-thunk. It also includes a mechanism to bypass the successful promise resolution as described below.

Install

npm install redux-promise-resolver

Setup

import promiseResolver from 'redux-promise-resolver';

const middewares = [  
  promiseResolver,
  // ... any other middleware
];

const configureStore = (initialState) => {
  return createStore(reducer, initialState, compose(
    applyMiddleware(...middewares)
    )
  );
};

Usage

The promise resolver processes any action that has a payload property that resolves to a promise. It will immediately dispatch <actionType>_REQUEST and then resolve the promise. If the promise resolves then <actionType> will be dispatched with the payload property set to the resolved value. If the promise is rejected then <actionType>_ERROR will be dispatched with the payload set to the error value.

For example:


const GET_POSTS = 'myapp/GET_POSTS';
const GET_POSTS_REQUEST = 'myapp/GET_POSTS_REQUEST';
const GET_POSTS_ERROR = 'myapp/GET_POSTS_ERROR';

// get posts action, postService.getPosts() returns a promise
export function getPosts() {
  return { type: GET_POSTS, payload: postService.getPosts() }
}

const initialState = {
  posts: null,
  pending: false;
  error: null;
}

// the reducer
function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case GET_POSTS_REQUEST:
      return {
        ...state,
        pending: true
      };

    case GET_POSTS:
      return {
        ...state,
        posts: action.payload,
        pending: false
      };

    case GET_POSTS_ERROR:
      return {
        ...state,
        error: action.payload,
        pending: false
      };

    default:
      return state;
  }
}

Passing request data

In addition to the promise passed in the payload property you can also pass data that will be dispatched in the <actionType>_REQUEST action by including a requestData property:

// get post action, postService.getPost(id) returns a promise
export function getPost(id) {
  return { type: GET_POSTS, payload: postService.getPosts(id), requestData: id }
}

// when the GET_POST_REQUEST action is dispatched the action.requestData will contain the id

If no requestData is present the action.requestData property will be set to null. The requestData property will also be included in the <actionType>_ERROR action.

Bypassing the action

This is useful for a case where you want to periodically dispatch an async action, but only update if something has changed. For example, you could do a background query for a list of items visible on the screen, show a spinner to indicate the background refresh, and then check the received data to see if there were any changes. By supplying a processor property that is a function in the action, it will be called when the promise resolves and passed the data. If the function returns true then the success action will be dispatched with the update data. If the function returns false then the <actionType>_BYPASS action will be dispatched instead. This could for example turn off the spinner without updating the data. See the testCases for a usage example.

Testing

npm run test