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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-promisify

v0.0.4

Published

> React data fetching made simple.

Readme

react-promisify

React data fetching made simple.

This project is a simple Higher Order Component wrapper around Promises in React. It allows you to define your API calls, initiate data fetching and consume results in elegant and clean way. State managing is done for you under the hood.

Table of Contents

Demo

The simplest way to start playing around with react-promisify is with this CodeSandbox snippet: https://codesandbox.io/

It covers basic Promise request definition and data fetching emitting.

Installation

Install it with yarn:

yarn add react-promisify

Or with npm:

npm install react-promisify --save

Getting started

Your component needs to be decorated by withPromise HOC in order to be able fetch the data and have an access to it. It accepts the name of the prop, containing response state as the first argument and a function, which returns Promise as a second.

Once you call this.props.movies.fetch(), your Promise function will be envoked and updated state variables will be passed into the component. Parameters, passed to fetch() method, will be automatically provided to your custom Promise function.

When request is in progress and not finished yet - isFetching variable will be equal to true. After request will be fetched, you'll get the response in data variable and timestamp of the request as lastUpdated value.

⚠️ Note, that it's not safe to rely only on isFetching variable when displaying loading indicators. On the first render, you will have isFetching:false, because data fetching starts after your component mounts. Additionally, you should check that lastUpdated variable is falsy, which is guaranteed when you render your component first time.

  import { withPromise } from 'react-promisify';

  class Movies extends React.Component {
    componentDidMount() {
      this.props.movies.fetch("comedy")
    }

    render() {
      const { isFetching, lastUpdated, data } = this.props.movies;

      return isFetching || !lastUpdated ? (
        <div>Loading...</div>
      ) : (
        <div>
          {data.map(movie => <div key={movie.id}>{movie.title}</div>)}
        </div>
      );
    }
  }

  const fetchMovies = (category) =>
    fetch(`http://your-amazing-api.com/movies/${category}`)
      .then(res => res.json())

  const DecoratedMovies = withPromise("movies", fetchMovies)(Movies);

Advanced usage

Using middlewares

Middlewares are useful when you need to hook up into the request/response lifecycle, make some actions before or after the response finished.

Basic middleware function is having the following signature:

(next) => (...args) => {}

It accepts next Promise function in the middleware chain as an input and returns new Promise function, which will be passed to the next middleware. Returned function, will take the same arguments as your initial data fetching call.

The simple logger middleware will look as the following:

const logger = (next) => (...args) => {
  console.log("Request was started with these arguments:", args);
  return next(...args).then(res => {
    console.log("Request was finished with:", res);
    return err;
  }).catch(err => {
    console.log("Request was failed with:", err);
    throw err;
  });
}

It could be applied by returning it from the function, passed at the 3rd argument of the HOC:

withPromise("movies", fetchMovies, props => logger);

Out of the box, the library is coming with onSuccess, onFailure and provideRequest middlewares.

Refer to the API reference for detailed information regarding each one.

import { compose } from 'ramda';

export default withPromise("movies", fetchMovies, props => compose(
  provideRequest(() => [props.userId]),
  onSuccess(() => {
    console.log("Request was succeded");
  }),
  onFailure(() => {
    console.log("Request was failed");
  })
))(Movies);

In the example above, compose function from Ramda is used for combining multiple middlewares into a single one.

Updating response data

Most likely, in a complex cases you will need to update fetched data state in response to some actions in future. For example, when new movie is created, you will want to update its list. For that case, .update() method of the provided request prop should be used:

import { compose } from 'ramda';

export default compose(
  withPromise("fetchMovies", fetchMovies),
  withPromise("createMovie", createMovie, props => onSuccess(movie => {
    props.update(movies => [...movies, movie]);
  }))
)(Movies);

compose function, from that example serves the same purpose - composing multiple HOC's into a new one.

Error handling

Roadmap

  • Default value support