# nanoguard

> Small module that allows you to guard a call to a function.

Latest version **1.3.0** (published 2020-02-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install nanoguard
pnpm add nanoguard
yarn add nanoguard
bun add nanoguard
```

## 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.3.0 |
| Published | 2020-02-14 |
| First published | 2019-09-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 64 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/nanoguard
- Repository: https://github.com/mafintosh/nanoguard
- Issues: https://github.com/mafintosh/nanoguard/issues
- npm.io page: https://npm.io/package/nanoguard

## Recent versions

- 1.3.0 (latest) — 2020-02-14
- 1.2.2 — 2019-11-10
- 1.2.1 — 2019-09-12
- 1.2.0 — 2019-09-12
- 1.1.0 — 2019-09-12
- 1.0.0 — 2019-09-12
- 0.0.0 — 2019-09-12

## README

# nanoguard

Small module that allows you to guard a call to a function.

```
npm install nanoguard
```

## Usage

``` js
const Nanoguard = require('nanoguard')
const guard = new Nanoguard()

guard.wait()

// When the amount of wait() calls reflect continue() calls ready is called
guard.ready(function () {
  console.log('Ready to continue!')
})

guard.continue()
```

## API

#### `const guard = new Nanoguard()`

Make a new guard instance

#### `guard.wait()`

Increment the wait counter.
Non-owners of the guard can use this to defer the ready function of the guard owner.

#### `guard.continue()`

Decrement the wait counter on the next tick. If the counter is `0` it calls all pending
ready functions.
If you called wait() you have to call continue() at some point.

#### `guard.continueSync()`

Same as `guard.continue()` but decrements in the same tick.

#### `const cont = guard.waitAndContinue()`

Calls wait and returns a function that when called calls continue() once no matter how many times it is called.

#### `guard.ready(fn)`

Pass a function that is called when the wait counter is `0`.

#### `guard.destroy()`

Force sets the wait counter to `0` forever.
Should only be called by the owner of the guard.

#### `const bool = guard.waiting`

Boolean indicating if the wait counter is `> 0`.

## License

MIT

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