Options
All
  • Public
  • Public/Protected
  • All
Menu

@rishiosaur/async

Index

Type aliases

AsyncEffect

AsyncEffect<T>: (mounted: boolean) => Promise<T>

An asynchronous effect that may return something.

Type parameters

  • T

    Type that the effect returns. Note: in a lot of cases, when used with useAsyncEffect, this can be void.

Type declaration

    • (mounted: boolean): Promise<T>
    • Parameters

      • mounted: boolean

      Returns Promise<T>

CleanupFunc

CleanupFunc<T>: (result: T | undefined) => void

Cleanup function used on destruction of component (on unmount).

Type parameters

  • T

    Type that the original effect returns (may be undefined, in the case that T is void).

Type declaration

    • (result: T | undefined): void
    • Parameters

      • result: T | undefined

      Returns void

DependencyList

DependencyList: readonly any[]

Dependency list for effects.

PromiseEffectState

PromiseEffectState<T>: { error: null; status: "pending"; value: null } | { error: null; status: "fulfilled"; value: T } | { error: Error; status: "rejected"; value: null }

Possible states of the Promise effect used in usePromiseEffect.

Type parameters

  • T

    Return type of the effect response.

Functions

useAsyncEffect

  • Base Asynchronous Effect Hook.

    export

    Type parameters

    • T

      Result of your effect.

    Parameters

    • effect: AsyncEffect<T>

      An effect function that returns Promise<T>. Accepts argument of mounted.

    • Optional dependencies: DependencyList

    Returns void

  • Asynchronous Effect Hook with support for a cleanup callback.

    export

    Type parameters

    • T

      Result of your effect.

    Parameters

    Returns void

usePromiseEffect

  • A Promise effect that builds on useAsyncEffect with state management built in.

    export
    example
     // In a component
     const [value, loading, err] = usePromiseEffect(() =>
    fetch("https://jsonplaceholder.typicode.com/todos/1").then((response) =>
    response.json()
    ) // Catching is automatically supported, and can be accessed through `err`.
    );

    Type parameters

    • T

      Result of [[effect]]

    Parameters

    Returns [T | null, boolean, Error | null]

    States of the promise at any given time.

Generated using TypeDoc