
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
@arcticzeroo/react-promise-hook
Advanced tools
React hooks for managing promise state. Provides a simple way to track the lifecycle of async operations — loading, success, and error — without boilerplate.
npm install @arcticzeroo/react-promise-hook
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.
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;
}
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.
// ❌ 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, orvalue and erroreslint.config.js)import promiseHook from '@arcticzeroo/react-promise-hook/eslint';
export default [
promiseHook.configs.recommended,
// ... your other config
];
.eslintrc){
"plugins": ["@arcticzeroo/react-promise-hook"],
"rules": {
"@arcticzeroo/react-promise-hook/require-promise-state-stage": "error"
}
}
FAQs
Promise hooks for react
We found that @arcticzeroo/react-promise-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.