Skip to content

Commit

Permalink
feat: add useAsync docs
Browse files Browse the repository at this point in the history
  • Loading branch information
itszeeshan committed Aug 29, 2024
1 parent 8a1f87b commit 259d119
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Here are some of the snippets you can use:
For detailed information about each hook, check out the following links:

- [useFetch Documentation](https://github.com/itszeeshan/react-custom-hooks/docs/useFetch.md)
- [useAsync Documentation](https://github.com/your-username/react-custom-hooks/docs/useAsync.md)
- [useAsync Documentation](https://github.com/itszeeshan/react-custom-hooks/docs/useAsync.md)

## Contributing

Expand Down
42 changes: 42 additions & 0 deletions docs/useAsync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
### `useAsync`

A hook to handle asynchronous tasks.

## Parameters

- `callback` (`function`): The asynchronous function to execute.
- `dependencies` (`array`, optional): Dependency array to control when the async function should be executed.

## Returns

- An object containing `data`, `error`, and `loading`.

## Hooks Involved
- [useState](https://react.dev/reference/react/useState)
- [useCallback](https://react.dev/reference/react/useCallback)
- [useEffect](https://react.dev/reference/react/useEffect)

## How to Use

```js
import useAsync from "./useAsync"

export default function AsyncComponent() {
const { loading, error, value } = useAsync(() => {
return new Promise((resolve, reject) => {
const success = false
setTimeout(() => {
success ? resolve("Hi") : reject("Error")
}, 1000)
})
})

return (
<div>
<div>Loading: {loading.toString()}</div>
<div>{error}</div>
<div>{value}</div>
</div>
)
}
```

0 comments on commit 259d119

Please sign in to comment.