Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 572 Bytes

useAsync.md

File metadata and controls

38 lines (28 loc) · 572 Bytes

useAsync

React hook that resolves an async function or a function that returns a promise;

Usage

import {useAsync} from 'react-use';

// Returns a Promise that resolves after one second.
const fn = () => new Promise((resolve) => {
  setTimeout(() => {
    resolve('RESOLVED');
  }, 1000);
});

const Demo = () => {
  const {loading, value, error} = useAsync(fn);

  return (
    <div>
      {loading
        ? <div>Loading...</div>
        : <div>Value: {value}</div>
      }
    </div>
  );
};

Reference

useAsync(fn, args?: any[]);