Options
All
  • Public
  • Public/Protected
  • All
Menu

use-async

styled with prettier Travis codecov dependencies Status

Installation

npm install @nutgaard/use-async --save

Usage

The library exposes one hook useAsync, and three utility-functions to help use the result (isPending, hasData and hasError).

import React from 'react';
import useAsync, { isPending, hasError } from '@nutgaard/use-async';

const source = React.useCallback((isRerun) => Promise.resolve("your data here"), []);
const result = useAsync(source);

if (isPending(result)) {
  return <Spinner />;
} else if (hasError(result)) {
  return <Error />
} 

return <pre>{result.data}</pre>

useAsync API

Argument Type Optional Default
source (isRerun) => Promise<DATA> No -
lazy boolean Yes false
dependencyList Array<any> Yes undefiend

In cases where dependencyList is defined it is passed on to useEffect instead of source. This allows a greater control of when the effect should run in cases where the source does not necessarily change.

Types

Full documentation of types can be seen here, or in the 80-ish lines of code.

Credits

Made using the awesome typescript library starter

Index

Type aliases

AsyncData

AsyncData<TYPE>: WithoutData | WithData<TYPE> | WithError

Type parameters

  • TYPE

AsyncResult

AsyncResult<TYPE>: AsyncData<TYPE> & object

Type parameters

  • TYPE

WithData

WithData<TYPE>: object

Type parameters

  • TYPE

Type declaration

WithError

WithError: object

Type declaration

  • error: any
  • status: ERROR

WithoutData

WithoutData: object

Type declaration

Functions

hasData

  • hasData<TYPE>(result: AsyncData<TYPE>): boolean

hasError

isLoading

isPending

  • isPending(result: AsyncData<any>, includeReloading?: boolean): boolean
  • Parameters

    • result: AsyncData<any>
    • Default value includeReloading: boolean = false

    Returns boolean

useAsync

  • useAsync<TYPE>(source: function, lazy?: boolean, dependencyList?: DependencyList, initialState?: AsyncData<TYPE>): AsyncResult<TYPE>
  • Type parameters

    • TYPE

    Parameters

    • source: function
        • (isRerun: boolean): Promise<TYPE>
        • Parameters

          • isRerun: boolean

          Returns Promise<TYPE>

    • Default value lazy: boolean = false
    • Optional dependencyList: DependencyList
    • Optional initialState: AsyncData<TYPE>

    Returns AsyncResult<TYPE>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc