# async-constructor

> The helper functions for creating classes that require asynchronous constructors.

Latest version **0.4.17** (published 2021-10-14) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.17 |
| Published | 2021-10-14 |
| First published | 2018-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 5.9 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | BlackGlory |
| Maintainers | black_glory |
| Keywords | AsyncConstructor, Constructor, TypeScript |

## Links

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

## Dependencies (3)

- [hotypes](https://npm.io/package/hotypes.md) ^0.4.2
- [justypes](https://npm.io/package/justypes.md) ^0.1.6
- [extra-promise](https://npm.io/package/extra-promise.md) ^0.19.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.4.17 (latest) — 2021-10-14
- 0.4.16 — 2021-07-12
- 0.4.15 — 2021-07-03
- 0.4.14 — 2021-05-17
- 0.4.13 — 2021-05-07
- 0.4.12 — 2021-03-27
- 0.4.11 — 2021-03-17
- 0.4.10 — 2021-03-07
- 0.4.9 — 2021-02-28
- 0.4.8 — 2021-02-04
- 0.4.5 — 2021-01-20
- 0.4.4 — 2021-01-15
- 0.4.3 — 2021-01-04
- 0.4.2 — 2020-08-09
- 0.4.1 — 2020-05-25
- … 4 more at https://npm.io/package/async-constructor/versions

## README

# async-constructor [![npm](https://img.shields.io/npm/v/async-constructor.svg?maxAge=86400)](https://www.npmjs.com/package/async-constructor) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/BlackGlory/async-constructor/master/LICENSE)

The helper functions for creating classes that require async constructors.

## Install

```sh
npm install --save async-constructor
# or
yarn add async-constructor
```

## Usage

This module is created for [TypeScript](#TypeScript), but can also be used in [JavaScript](#JavaScript).

### JavaScript

You may not need the module because JavaScript constructor could return a `Promise<this>`:

```js
class Resource {
  constructor(url) {
    return (async () => {
      this.content = await load(url)
      return this
    })()
  }
}
```

or

```js
function Resource(url) {
  return (async () => {
    this.content = await load(url)
    return this
  })()
}
```

### TypeScript

#### AsyncConstructor

##### ES2017

```ts
import { AsyncConstructor } from 'async-constructor'

class Resource extends AsyncConstructor {
  content!: string

  constructor(url: string) {
    super(async () => {
      this.content = await load(url)
    })
  }
}

const resource = await new Resource('data.txt')
```

##### ES2015

```ts
import { AsyncConstructor } from 'async-constructor/lib/es2015'

class Resource extends AsyncConstructor {
  content!: any

  constructor(url: string) {
    super(async function(this: Resource) {
      this.content = await load(url)
    })
  }
}

;(async () => {
  const resource = await new Resource('data.txt')
})()
```

#### appendAsyncConstructor

```ts
function appendAsyncConstructor<T, U extends any[]>(
  target: T
, asyncConstructor: (...args: U) => PromiseLike<void>
, args?: U
): void
```

Once a class has an async constructor, its subclasses will also have async constructors.

The function `AsyncConstructor` is a wrapper for `appendAsyncConstructor`, you can call `appendAsyncConstructor` in the sync constructor to append an async constructor.

```js
import { appendAsyncConstructor } from 'async-constructor'

class Resource {
  content!: string

  constructor(url: string) {
    appendAsyncConstructor(this, async () => {
      this.content = await load(url)
    })
  }
}

class DataResource extends Resource {
  constructor(append: string) {
    super('data.txt')

    appendAsyncConstructor(this, async () => {
      this.content += append
    })
  }
}

const resource = await new DataResource('\n')
```

The multiple async constructors will be called in order, and always called after all sync constructor.

#### mixinAsyncConstructor

```ts
function mixinAsyncConstructor<Base extends Constructor>(
  base: Base
, asyncConstructor: (...args: ConstructorParameters<Base>) => PromiseLike<void>
): new (...args: ConstructorParameters<Base>) => PromiseLike<ReturnTypeOfConstructor<Base>>
```

The mixin function `mixinAsyncConstructor` is also used to add async constructors to classes, but you cannot access protected/private member in the async constructor.

```ts
import { mixinAsyncConstructor } from 'async-constructor'

class Base {
  content!: string
  constructor(url: string) {}
}

const Resource = mixinAsyncConstructor(Base , async function(this: Base, url) {
  this.content = await load(url)
})

const resource = await new Resource('data.txt')
```

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