mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-26 00:16:51 +00:00
22 lines
589 B
TypeScript
22 lines
589 B
TypeScript
import type { Inputs } from 'preact/hooks'
|
|
import { useEffect } from 'preact/hooks'
|
|
import type { AsyncCancel, AsyncState } from './index.js'
|
|
import { useAsyncFn } from './index.js'
|
|
|
|
export function useAsync<R>(
|
|
fn: () => Promise<R | typeof AsyncCancel>,
|
|
inputs: Inputs = [],
|
|
initialState: AsyncState<R> = { loading: true },
|
|
): AsyncState<R> & { refresh: () => Promise<unknown> } {
|
|
const [state, callback] = useAsyncFn<R, () => Promise<R | typeof AsyncCancel>>(fn, inputs, initialState)
|
|
|
|
useEffect(() => {
|
|
callback()
|
|
}, [callback])
|
|
|
|
return {
|
|
...state,
|
|
refresh: callback,
|
|
}
|
|
}
|