# @artcom/async-task-hook

> A react hook to perform async tasks.

Latest version **1.2.0** (published 2022-04-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @artcom/async-task-hook
pnpm add @artcom/async-task-hook
yarn add @artcom/async-task-hook
bun add @artcom/async-task-hook
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2022-04-06 |
| First published | 2020-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | j-era, becks-artcom, xadhoom, fabue, utileetdulce |
| Keywords | react, hook, async, task |

## Links

- npm: https://www.npmjs.com/package/@artcom/async-task-hook
- Repository: https://github.com/artcom/async-task-hook
- Homepage: https://github.com/artcom/async-task-hook#readme
- Issues: https://github.com/artcom/async-task-hook/issues
- npm.io page: https://npm.io/package/@artcom/async-task-hook

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2022-04-06
- 1.1.3 — 2021-01-14
- 1.1.2 — 2020-09-03
- 1.1.1 — 2020-06-17
- 1.1.0 — 2020-06-14
- 1.0.0 — 2020-06-12

## README

# Async Task Hook

Provides an react hook for async tasks.

## Features

Runs a given async task in a [useEffect](https://reactjs.org/docs/hooks-reference.html#useeffect) hook. The returned state contains the status of the runnning task as well as the result or error. The task completion will be ignored if the component has been unmounted.

Note: The given task function must not change (e.g. wrapped with [useCallback](https://reactjs.org/docs/hooks-reference.html#usecallback)) otherwise a new task (read [useEffect](https://reactjs.org/docs/hooks-reference.html#useeffect)) will be started and the previous result will be ignored.

## States

* `RUNNING` the initial state and the state while the task is running.
* `FINISHED` the final state as soon as the method has finished.
* `ERROR` the final state if the task threw an error.

## Example

This example queries content with [axios](https://github.com/axios/axios). The component receives the `url` as property and creates a persistent `queryFunc` which is fed to the async task hook. The returned result can be accessed when the async task has `FINISHED`. If the method throws an exception the status will be set to `ERROR`

```javascript
import axios from 'axios'
import React, { useCallback } from "react"
import { useAsyncTask, RUNNING, FINISHED, ERROR } from "@artcom/async-task-hook"

const MyComponent = ({ url }) => {
  const queryFunc = useCallback(() => axios.get(url), [url])
  const query = useAsyncTask(queryFunc)

  switch(query.status) {
    case RUNNING: return <>Loading...</>
    case FINISHED: return <>query.result</>
    case ERROR: return <>query.error</>
  }
}
```

## Tests

Checkout the [tests](./test/useAsyncTask.test.js) for different scenarios.

```bash
npm i && npm run test
```

---
_Source: https://npm.io/package/@artcom/async-task-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
