mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
39 lines
572 B
Markdown
39 lines
572 B
Markdown
# `useAsync`
|
|
|
|
React hook that resolves an `async` function or a function that returns
|
|
a promise;
|
|
|
|
|
|
## Usage
|
|
|
|
```jsx
|
|
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
|
|
|
|
```ts
|
|
useAsync(fn, args?: any[]);
|
|
```
|