# await-lock

> Mutex locks for async functions

Latest version **3.0.0** (published 2025-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install await-lock
pnpm add await-lock
yarn add await-lock
bun add await-lock
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2025-02-24 |
| First published | 2015-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 95 |
| Author | James Ide |
| Maintainers | ide |
| Keywords | async, await, lock, es2017, co, yield, generator, promise |

## Links

- npm: https://www.npmjs.com/package/await-lock
- Repository: https://github.com/ide/await-lock
- Issues: https://github.com/ide/await-lock/issues
- npm.io page: https://npm.io/package/await-lock

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2025-02-24
- 2.2.2 — 2022-04-29
- 2.2.1 — 2022-04-22
- 2.2.0 — 2022-04-22
- 2.1.0 — 2020-09-11
- 2.0.1 — 2019-11-04
- 2.0.0 — 2019-09-12
- 1.2.1 — 2019-09-12
- 1.2.0 — 2019-09-07
- 1.1.3 — 2018-05-31
- 1.1.2 — 2016-11-16
- 1.1.1 — 2015-05-30
- 1.1.0 — 2015-02-06
- 1.0.0 — 2015-02-06
- 0.0.1 — 2015-02-06
- … 1 more at https://npm.io/package/await-lock/versions

## README

# AwaitLock ![tests](https://github.com/ide/await-lock/workflows/Tests/badge.svg) [![codecov](https://codecov.io/gh/ide/await-lock/branch/master/graph/badge.svg)](https://codecov.io/gh/ide/await-lock)
Mutex locks for async functions

# API

[API documentation](https://github.com/ide/await-lock/wiki/API-documentation)

# Usage

This package is published only as an ES module. In addition to importing ES modules from ES modules, modern versions of Node.js support [requiring ES modules from CommonJS modules](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require).

```javascript
import AwaitLock from 'await-lock';

let lock = new AwaitLock();

async function runSerialTaskAsync() {
  await lock.acquireAsync();
  try {
    // IMPORTANT: Do not return a promise from here because the finally clause
    // may run before the promise settles, and the catch clause will not run if
    // the promise is rejected
  } finally {
    lock.release();
  }
}
```

You can also use AwaitLock with [co](https://github.com/tj/co) and generator functions.

```javascript
import AwaitLock from 'await-lock';

let runSerialTaskAsync = co.wrap(function*() {
  yield lock.acquireAsync();
  try {
    // Run async code in the critical section
  } finally {
    lock.release();
  }
});
```

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