:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useCallback, useRef, useState } from 'react'
export interface AsyncAction<Args extends unknown[]> {
run: (...args: Args) => Promise<void>
pending: boolean
}
// A second call while one is in flight is ignored, which guards against double taps.
export function useAsyncAction<Args extends unknown[]>(fn: (...args: Args) => Promise<void> | void): AsyncAction<Args> {
const [pending, setPending] = useState(false)
const inFlight = useRef(false)
const fnRef = useRef(fn)
fnRef.current = fn
const run = useCallback(async (...args: Args): Promise<void> => {
if (inFlight.current) {
return
}
inFlight.current = true
setPending(true)
try {
await fnRef.current(...args)
} finally {
inFlight.current = false
setPending(false)
}
}, [])
return { run, pending }
}