UNPKG

@react-hookz/web

Version:

React hooks done right, for browser and SSR.

41 lines (40 loc) 1.5 kB
export declare type IAsyncStatus = 'loading' | 'success' | 'error' | 'not-executed'; export declare type IAsyncState<Result> = { status: 'not-executed'; error: undefined; result: Result; } | { status: 'success'; error: undefined; result: Result; } | { status: 'error'; error: Error; result: Result; } | { status: IAsyncStatus; error: Error | undefined; result: Result; }; export interface IUseAsyncActions<Result, Args extends unknown[] = unknown[]> { /** * Reset state to initial, when async function haven't been executed. */ reset: () => void; /** * Execute async function manually. */ execute: (...args: Args) => Promise<Result>; } export interface IUseAsyncMeta<Result, Args extends unknown[] = unknown[]> { /** * Recent promise returned from async function. */ promise: Promise<Result> | undefined; /** * List of arguments applied to recent async function invocation. */ lastArgs: Args | undefined; } export declare function useAsync<Result, Args extends unknown[] = unknown[]>(asyncFn: (...params: Args) => Promise<Result>, initialValue: Result): [IAsyncState<Result>, IUseAsyncActions<Result, Args>, IUseAsyncMeta<Result, Args>]; export declare function useAsync<Result, Args extends unknown[] = unknown[]>(asyncFn: (...params: Args) => Promise<Result>, initialValue?: Result): [IAsyncState<Result | undefined>, IUseAsyncActions<Result, Args>, IUseAsyncMeta<Result, Args>];