Options
All
  • Public
  • Public/Protected
  • All
Menu

@bytesoftio/use-async

Installation

yarn add @bytesoftio/use-async or npm install @bytesoftio/use-async

Table of contents

Description

A convenient hook to deal with async operations inside React components.

useAsync

Function useAsync takes any other function that returns anything that can be awaited and returns its result as soon as the promise resolves. You also get some useful things like a loading indicator, the possible error that might have been thrown / or occurred through rejection and a reload function to rerun the async procedure. Async action can also be canceled with cancel or resolved directly using resolve.

import React from "react"
import { useAsync } from "@bytesoftio/use-async"

const fetchCommodityPrice = (commodity: string) => {
  return new Promise<string>((resolve) => {
    setTimeout(() => resolve(`${commodity}: 1000$`), 3000)
  })
}

const Component = () => {
  const handle = useAsync(() => fetchCommodityPrice("gold"))

  if (handle.loading) {
    return <span>Loading...</span>
  }

  if (handle.error) {
    return <span>There was an error :(</span>
  }

  return (
    <div>
      <span>{handle.result}</span>
      {/* reload commodity price */}
      <button onClick={() => handle.reload()}>reload</button>
      {/* fetch price for another commodity by replacing */}
      {/* the async action with a slightly different one */}
      <button onClick={() => handle.reload(() => fetchCommodityPrice("silver"))}>fetch price for silver</button>
    </div>
  )
}

Index

Type aliases

AsyncAction

AsyncAction<TResult>: () => Promise<TResult> | TResult

Type parameters

  • TResult

Type declaration

    • (): Promise<TResult> | TResult
    • Returns Promise<TResult> | TResult

AsyncCancel

AsyncCancel: () => void

Type declaration

    • (): void
    • Returns void

AsyncHandle

AsyncHandle<TResult>: AsyncStatus<TResult> & { cancel: AsyncCancel; reload: AsyncReload<TResult>; resolve: AsyncResolve<TResult> }

Type parameters

  • TResult

AsyncReload

AsyncReload<TResult>: (action?: AsyncAction<TResult>) => Promise<TResult | undefined> | TResult | undefined

Type parameters

  • TResult

Type declaration

    • (action?: AsyncAction<TResult>): Promise<TResult | undefined> | TResult | undefined
    • Parameters

      Returns Promise<TResult | undefined> | TResult | undefined

AsyncResolve

AsyncResolve<TResult>: (result: TResult) => void

Type parameters

  • TResult

Type declaration

    • (result: TResult): void
    • Parameters

      • result: TResult

      Returns void

AsyncStatus

AsyncStatus<TResult>: { cancelled: boolean; error: any | undefined; errored: boolean; loading: boolean; result: TResult | undefined }

Type parameters

  • TResult

Type declaration

  • cancelled: boolean
  • error: any | undefined
  • errored: boolean
  • loading: boolean
  • result: TResult | undefined

UseAsync

UseAsync: <TResult>(action?: AsyncAction<TResult>, dependencies?: any[]) => AsyncHandle<TResult>

Type declaration

Functions

Const useAsync

  • useAsync<TResult>(action?: AsyncAction<TResult>, dependencies?: any): { cancel: () => void; reload: (action?: AsyncAction<TResult>) => Promise<TResult | undefined> | TResult | undefined; resolve: (Anonymous function) }
  • Type parameters

    • TResult

    Parameters

    • Optional action: AsyncAction<TResult>
    • Default value dependencies: any = [] as any

    Returns { cancel: () => void; reload: (action?: AsyncAction<TResult>) => Promise<TResult | undefined> | TResult | undefined; resolve: (Anonymous function) }

    • cancel: () => void
        • (): void
        • Returns void

    • reload: (action?: AsyncAction<TResult>) => Promise<TResult | undefined> | TResult | undefined
        • (action?: AsyncAction<TResult>): Promise<TResult | undefined> | TResult | undefined
        • Parameters

          Returns Promise<TResult | undefined> | TResult | undefined

    • resolve: (Anonymous function)