# await-mutex

> Promised based Mutex

Latest version **1.0.2** (published 2018-05-07) · Unlicense license · 0 weekly downloads

## Install

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

## 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.0.2 |
| Published | 2018-05-07 |
| First published | 2015-12-24 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 41 |
| Author | Mauro Titimoli |
| Maintainers | mgtitimoli |
| Keywords | async, await, block, lock, locking, mutex, mutual-exclusion, parallel, semaphore, sync, synchronize, synchronization, unlock, unlocking |

## Links

- npm: https://www.npmjs.com/package/await-mutex
- Repository: https://github.com/mgtitimoli/await-mutex
- Homepage: https://github.com/mgtitimoli/await-mutex#readme
- Issues: http://github.com/mgtitimoli/await-mutex/issues
- npm.io page: https://npm.io/package/await-mutex

## Dependencies (1)

- [babel-runtime](https://npm.io/package/babel-runtime.md) ~6.3.0

## 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

- 1.0.2 (latest) — 2018-05-07
- 1.0.1 — 2015-12-24

## README

# await-mutex

Promised based Mutex for cases where you need to synchronize sequentially the access to a single resource from multiple locations.

A typical use case for a mutex is when multiple asynchronous processes are fired and all of them have to execute another, **but the same**, asynchronous process as they arrive, one at a time, waiting for previous call (if any) to finish before calling it again.

## Examples

### file-appender.js

Let you create an object to perform file appends **one at a time**.

```js
import * as fs from "fs";
import Mutex from "await-mutex";

export default class FileAppender {

    constructor(filename) {

        this._filename = filename;
        this._mutex = new Mutex();
    }

    async append(data, options = undefined) {

        let unlock = await this._mutex.lock();

        fs.appendFile(this._filename, data, options, error => {

            unlock();

            if (error) {
                throw error;
            }
        });
    }
}
```

## API

```js
import Mutex from "await-mutex";
```

### Mutex

Creates an instance of Mutex (can not be called without **new**).

```js
let mutex = new Mutex();
```

### Mutex.prototype.isLocked

Returns if the mutex instance is (true) or not locked (false).

```js
let unlock = await mutex.lock();

console.log(mutex.isLocked()); // prints true
```

### Mutex.prototype.lock: Promise

- Waits until the mutex is unlocked and then locks it.
- It returns an [ES2015 standard Promise](https://tc39.github.io/ecma262/#sec-promise-objects) (this allows the use of [async/await](http://tc39.github.io/ecmascript-asyncawait/)) which gets resolved once the mutex is unlocked.
- The promise resolution value is an **unlock function** that has to be called once the mutex needs to be unlocked.

```js
async function someFunc(mutex) {

    let unlock = await mutex.lock(); // wait until mutex is unlocked

    setTimeout(unlock, 3000);

    console.log(someFunc.name);
}

async function someOtherFunc(mutex) {

    let unlock = await mutex.lock(); // wait until mutex is unlocked

    console.log(someOtherFunc.name);
}

let mutex = new Mutex();

someFunc(mutex); // prints SomeFunc inmediately
someOtherFunc(mutex); // waits 3 secs for mutex to be unlocked and then prints SomeOtherFunc
```

## Installation

With [npm](http://npmjs.org) do:

```
npm install --save await-mutex
```

## Contributing

### Contributors

- [Mauro Titimoli](https://github.com/mgtitimoli)

### How to

Take a look to the [Contributing Guide](CONTRIBUTING.md)

## license

[Unlicense](http://unlicense.org/).

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