# async-property

> Async property

Latest version **0.1.0** (published 2020-03-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-property
pnpm add async-property
yarn add async-property
bun add async-property
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-03-30 |
| First published | 2019-09-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alex Arus |
| Maintainers | alexarus |
| Keywords | async, property |

## Links

- npm: https://www.npmjs.com/package/async-property
- Repository: https://github.com/ArusSoft/async-property
- Homepage: https://github.com/ArusSoft/async-property#readme
- Issues: https://github.com/ArusSoft/async-property/issues
- npm.io page: https://npm.io/package/async-property

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2020-03-30
- 0.0.2 — 2019-09-11
- 0.0.1 — 2019-09-03

## README

# async-property
Simple typesafe helper to store async operations state and data.

# Example
To work with async processes in react and redux we use async types and actions, so why not to use async properties.
```tsx
import React, { useState, useEffect } from 'react'
import {
  AsyncProperty,
  emptyProperty, requestProperty,
  setSuccessProperty, setFailureProperty,
  isEmpty, isRequest, isSuccess, isFailure
} from 'async-property';

const requestStringProperty = async () => new Promise<string>((resolve) => resolve('async result'))

const Example: React.FC = () => {
  const [stringProperty, setStringProperty] = useState<AsyncProperty<string>>(emptyProperty)

  useEffect(() => {
    setStringProperty(requestProperty)

    const fetchData = async () => {
      try {
        const stringResult = await requestStringProperty()
        setStringProperty(setSuccessProperty(stringResult))
      } catch (error) {
        setStringProperty(setFailureProperty(error))
      }
    }
    fetchData()
  }, [])

  if (isEmpty(stringProperty) || isRequest(stringProperty)) {
    return (
      <p>Processing</p>
    )
  }

  if (isFailure(stringProperty)) {
    return (
      <p>Failure: {stringProperty.error}</p>
    )
  }

  if (isSuccess(stringProperty)) {
    return (
      <p>Success: {stringProperty.value}</p>
    )
  }

  return <p>Initialize</p>
}
```

# Documentation
## Types
```typescript
type AsyncProperty<T, F = Error> = EmptyProperty | RequestProperty | SuccessProperty<T> | FailureProperty<F> | CancelProperty;
type EmptyProperty = {
    state: PropertyState.EMPTY
}
type RequestProperty = {
    state: PropertyState.REQUEST
}
type SuccessProperty<T> = {
    state: PropertyState.SUCCESS,
    value: T,
}
type FailureProperty<T = Error> = {
    state: PropertyState.FAILURE,
    error: T,
}
type CancelProperty = {
    state: PropertyState.CANCEL,
    reason?: string,
}
```
## Enum
```typescript
const enum PropertyState {
    EMPTY = 'Empty',
    REQUEST = 'Request',
    SUCCESS = 'Success',
    FAILURE = 'Failure',
    CANCEL = 'Cancel',
}
```
## Constants
```typescript
const emptyProperty: EmptyProperty = {
    state: PropertyState.EMPTY
}
const requestProperty: RequestProperty = {
    state: PropertyState.REQUEST
}
const cancelProperty: CancelProperty = {
    state: PropertyState.CANCEL
}
``` 
## Methods
### Setters
```typescript
function setSuccessProperty<T>(value: T): SuccessProperty<T>
function setFailureProperty<T>(error: T): FailureProperty<T>
function setCancelProperty(reason: string): CancelProperty
```
### Type check
```typescript
function isAsyncProperty<T, F>(property: any): property is AsyncProperty<T, F>
function isEmpty(property: AsyncProperty<any>): property is EmptyProperty
function isRequest(property: AsyncProperty<any>): property is RequestProperty
function isSuccess<T>(property: AsyncProperty<T>): property is SuccessProperty<T>
function isFailure<T>(property: AsyncProperty<any, T>): property is FailureProperty<T>
function isCancel(property: AsyncProperty<any>): property is CancelProperty
```

# Contribute

## Build
```
npm run build
```

## Test
```
npm test
```

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