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

@mckabue/react-use-async

v1.0.0

Published

A lightweight React hook for managing async operations with loading states, error handling, and data merging.

Readme

@mckabue/react-use-async

A lightweight React hook for managing async operations with loading states, error handling, and data merging.

npm version npm downloads license

Why This Hook?

Managing async state in React often means writing the same boilerplate: isLoading, error, data, try/catch — over and over. @mckabue/react-use-async encapsulates this pattern into a single, type-safe hook.

What you get:

  • Auto-execute on mount (or opt out with useDelayedAsync)
  • Loading statesisExecuting, isContinuing, isLoading
  • Error capture — errors are caught and exposed, not swallowed
  • Data mergingcontinueWith supports pagination / incremental loading
  • Argument trackingargs exposes the last-used arguments
  • Zero dependencies (except React)
  • Full TypeScript generics for type-safe data and arguments

Installation

npm install @mckabue/react-use-async
# or
yarn add @mckabue/react-use-async
# or
pnpm add @mckabue/react-use-async

Peer Dependencies:

  • react: ^18.0.0 || ^19.0.0

Usage

Basic — Execute on Mount

import { useAsync } from '@mckabue/react-use-async';

function UserProfile({ userId }: { userId: string }) {
  const { data: user, isLoading, error } = useAsync(
    () => fetch(`/api/users/${userId}`).then(r => r.json()),
    [userId], // re-fetches when userId changes
  );

  if (isLoading) return <Spinner />;
  if (error) return <ErrorMessage error={error} />;
  return <div>{user.name}</div>;
}

Delayed — Execute on Demand

import { useDelayedAsync } from '@mckabue/react-use-async';

function DeleteButton({ userId }: { userId: string }) {
  const { execute, isLoading } = useDelayedAsync(
    async (id: string) => {
      await fetch(`/api/users/${id}`, { method: 'DELETE' });
    },
  );

  return (
    <button onClick={() => execute(userId)} disabled={isLoading}>
      {isLoading ? 'Deleting...' : 'Delete User'}
    </button>
  );
}

Pagination — Continue With Merge

import { useAsync } from '@mckabue/react-use-async';

function InfiniteList() {
  const { data, isLoading, isContinuing, continueWith } = useAsync(
    () => fetchPage(1),
  );

  const loadMore = continueWith(
    () => fetchPage(nextPage),
    (oldData, newData) => ({
      items: [...(oldData?.items ?? []), ...(newData?.items ?? [])],
      nextPage: newData?.nextPage,
    }),
  );

  return (
    <div>
      {data?.items.map(item => <Item key={item.id} {...item} />)}
      <button onClick={loadMore} disabled={isContinuing}>
        {isContinuing ? 'Loading...' : 'Load More'}
      </button>
    </div>
  );
}

API Reference

useAsync(asyncCallback, dependencies?, executeOnMount?)

| Parameter | Type | Default | Description | |-----------|------|---------|-------------| | asyncCallback | (...args: A) => Promise<R> | — | The async function to execute | | dependencies | DependencyList | [] | React dependency list (triggers re-execution) | | executeOnMount | boolean | true | Whether to run on mount |

Returns: AsyncResponseType<R, A>

| Property | Type | Description | |----------|------|-------------| | data | R \| null | Resolved data | | isExecuting | boolean | Primary execution in progress | | isContinuing | boolean | Continuation in progress | | isLoading | boolean | Any operation in progress | | args | A | Arguments from last execute() call | | error | Error \| null | Error from last operation | | execute | (...args: A) => Promise<void> | Manually trigger the async callback | | continueWith | (cb, merger) => () => Promise<void> | Continue with data merging |

useDelayedAsync(asyncCallback, dependencies?)

Same as useAsync but with executeOnMount set to false. Perfect for user-triggered operations.

TypeScript Support

Fully typed with generics:

// Type-safe response and arguments
const { data, execute } = useAsync<User[], [string]>(
  async (query: string) => searchUsers(query),
  [],
  false,
);

// data is User[] | null
// execute accepts (query: string) => Promise<void>

License

MIT © Kabui Charles