# @mischnic/async-hooks

> An Async Hooks polyfill for Node less than 8, written in TypeScript.

Latest version **0.0.4** (published 2018-04-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @mischnic/async-hooks
pnpm add @mischnic/async-hooks
yarn add @mischnic/async-hooks
bun add @mischnic/async-hooks
```

## 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.0.4 |
| Published | 2018-04-22 |
| First published | 2018-04-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 59.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Credit Karma |
| Maintainers | mischnic |
| Keywords | async, typescript, async hooks, async wrap |

## Links

- npm: https://www.npmjs.com/package/@mischnic/async-hooks
- Repository: https://github.com/creditkarma/async-hooks
- Homepage: https://github.com/creditkarma/async-hooks#readme
- Issues: https://github.com/creditkarma/async-hooks/issues
- npm.io page: https://npm.io/package/@mischnic/async-hooks

## Dependencies (1)

- [es6-shim](https://npm.io/package/es6-shim.md) ^0.35.3

## 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.0.4 (latest) — 2018-04-22

## README

# Async Hooks

An Async Hooks polyfill for Node < 8, written in TypeScript.

The idea is to provide an interface identical to the native Async Hooks implementation in older versions of Node. If you `import` the library in Node 8+ it will return the native Async Hooks, otherwise it will return the polyfill.

The docs for Async Hooks [here](https://nodejs.org/api/async_hooks.html).

This implementation is based largely on the work of [Andreas Madsen](https://github.com/AndreasMadsen/async-hook)

## Install

```sh
$ npm install --save @creditkarma/async-hooks
```

## Usage

### `createHook`

Creates a new `AsyncHook` object with the supplied callbacks

```typescript
import * as AsyncHooks from '@creditkarma/async-hooks'

AsyncHooks.createHook({
    init(asyncId: number, type: string, triggerAsyncId: number, resource: object): void {
        // A new async resouce was created
    },
    before(asyncId: number): void {
        // The callback for async resource will be called
    },
    after(asyncId: number): void {
        // The callback for async resource was called
    },
    destroy(asyncId: number): void {
        // The async resource will be garbage collected
    }
}).enable()
```

In the native implementation of Async Hooks the `resource` received by the `init` method is the async object that was created. In the polyfill this `resource` is likely to not useful and is here for completeness. Usually this will be an empty wrapper object.

### `executionAsyncId`

Returns the unique ID of the currently executing async context.

```typescript
import * as AsyncHooks from '@creditkarma/async-hooks'

const currentAsyncId: number = AsyncHooks.executionAsyncId()
```

### `triggerAsyncId`

Returns the unique ID of the parent context for the currently executing async context.

```typescript
import * as AsyncHooks from '@creditkarma/async-hooks'

const parentAsyncId: number = AsyncHooks.triggerAsyncId()
```

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