SwipeUI
DocsHooks & utilities

useAsyncAction

Run an operation with pending, success and error state, and ignore stale completions.

Latest-run async state with minimum pending time, reset timing and unmount-safe callbacks.

Free (MIT)Version 1.0.0Released 4 Nov 2026iOS and Android

When to use it

  • Share pending, success and error state across an operation-driven control.

When not to use it

  • Use local state for a synchronous toggle with no operation lifecycle.

Installation

Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.

npx @swipeui/cli add use-async-action
  • Adds components/swipeui/hooks/use-async-action.ts.

Usage

components/save-draft.tsx
import { useAsyncAction } from '@/components/swipeui/hooks/use-async-action';

const action = useAsyncAction(saveDraft, { minPending: 450, resetAfter: 1800 });
// Call action.run(...args) from an event handler; read action.status and action.error.

run accepts synchronous or promise-returning handlers. A newer run wins; reset and unmount invalidate old results. The hook does not abort the underlying work. run resolves its result after success, or undefined for failure or a stale run. The control owns feedback, haptics and announcements.

Settings

OptionDefaultBehaviour
minPending450Minimum pending visibility in milliseconds, measured from the start of the run.
resetAfterunsetMilliseconds to retain a result before idle; unset retains it until reset or the next run.
onSuccessunsetLatest successful completion, after pending time.
onErrorunsetLatest failure, after pending time.

Both delays must be finite and non-negative. These are content timings, not animation durations. reset() cancels timers and clears error state. Keep event handlers guarded against accidental duplicate presses when appropriate; the hook deliberately permits overlapping runs for latest-run workflows.

Accessibility

  • The consuming component owns labels, busy states and announcements.

Motion

  • Static on its own; any movement comes from the parent that animates it.
  • Content timing only; components choose their own motion.

Performance

  • Run tokens ignore stale results; timers are cleared on reset and unmount. Underlying work is not aborted.

Notes for AI

  • run resolves the successful result or undefined on failure/stale completion. Handle outcomes through status and callbacks.

Compatibility

Runs onSupported
Expo SDK 57Yes
Expo SDK 58Yes
React Native CLI (with Expo modules)Yes
Expo GoYes
iOSYes
AndroidYes

This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.

Related items

On this page