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

@arcticzeroo/react-promise-hook

v1.5.0

Published

Promise hooks for react

Readme

@arcticzeroo/react-promise-hook

React hooks for managing promise state. Provides a simple way to track the lifecycle of async operations — loading, success, and error — without boilerplate.

Installation

npm install @arcticzeroo/react-promise-hook

Hooks

useImmediatePromiseState<T>(returnsPromise: () => Promise<T>): IRunnablePromiseState<T>

Runs the promise immediately when the callback changes. Wrap your callback in useCallback to avoid infinite loops.

const loadUsers = useCallback(() => fetch('/api/users').then(r => r.json()), []);
const { value, error, stage, run } = useImmediatePromiseState(loadUsers);

if (stage === PromiseStage.error) {
    return <RetryButton onClick={run} />;
}

if (stage !== PromiseStage.success) {
    return <Spinner />;
}

return <UserList users={value} />;

useDelayedPromiseState<T>(returnsPromise: () => Promise<T>, keepLastValue?: boolean): IDelayedPromiseState<T>

Like useImmediatePromiseState, but does not run automatically — call run() yourself. When keepLastValue is true (the default), the previous value is preserved during re-fetches to avoid UI flashing.

Returns an additional actualStage field reflecting the real-time stage, while stage holds the last completed stage when keepLastValue is enabled.

const search = useCallback(() => fetch(`/api/search?q=${query}`).then(r => r.json()), [query]);
const { value, stage, actualStage, run } = useDelayedPromiseState(search);

useEffect(() => { run(); }, [run]);

useExistingPromiseState<T>(promise: Promise<T>): IPromiseState<T>

Tracks state of an already-existing promise. Stage starts at running.

useMaybeExistingPromiseState<T>(promise: Promise<T> | undefined | null, initialStage?: PromiseStage): IPromiseState<T>

Like useExistingPromiseState, but accepts undefined/null. Stage starts at initialStage (default: notRun) until a promise is provided.

Types

enum PromiseStage {
    notRun,
    running,
    error,
    success,
}

interface IPromiseState<T> {
    stage: PromiseStage;
    value: T | undefined;
    error: any;
}

interface IRunnablePromiseState<T> extends IPromiseState<T> {
    run: () => void;
}

interface IDelayedPromiseState<T> extends IRunnablePromiseState<T> {
    actualStage: PromiseStage;
}

ESLint Plugin

The package ships with an ESLint rule to catch a common mistake: destructuring only value from a promise state hook without handling errors. This leads to silently ignored failures and missing loading states.

The Problem

// ❌ Bad: error and loading states are silently ignored
const { value } = useImmediatePromiseState(fetchData);
return <div>{value}</div>;

// ✅ Good: stage is checked to handle all states
const { value, stage } = useImmediatePromiseState(fetchData);

// ✅ Also good: both value and error are destructured
const { value, error } = useImmediatePromiseState(fetchData);

The require-promise-state-stage rule enforces that when destructuring a promise state hook, you must include either:

  • stage or actualStage, or
  • both value and error

Setup

Flat config (eslint.config.js)

import promiseHook from '@arcticzeroo/react-promise-hook/eslint';

export default [
    promiseHook.configs.recommended,
    // ... your other config
];

Legacy config (.eslintrc)

{
    "plugins": ["@arcticzeroo/react-promise-hook"],
    "rules": {
        "@arcticzeroo/react-promise-hook/require-promise-state-stage": "error"
    }
}