# rwmutex

> Read-Write Mutex with JavaScript Promises. Inspired by Golang's sync.RWMutex.

Latest version **1.0.0** (published 2017-09-19) · MIT license · 0 weekly downloads

## Install

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

## 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.0 |
| Published | 2017-09-19 |
| First published | 2017-09-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Tyler Johnson |
| Maintainers | mrgalaxy |

## Links

- npm: https://www.npmjs.com/package/rwmutex
- Repository: https://github.com/tyler-johnson/rwmutex
- Homepage: https://github.com/tyler-johnson/rwmutex#readme
- Issues: https://github.com/tyler-johnson/rwmutex/issues
- npm.io page: https://npm.io/package/rwmutex

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^3.0.1

## Recent versions

- 1.0.0 (latest) — 2017-09-19

## README

# RWMutext

[![npm](https://img.shields.io/npm/v/rwmutex.svg)](https://www.npmjs.com/package/rwmutex) [![Build Status](https://travis-ci.org/tyler-johnson/rwmutex.svg?branch=master)](https://travis-ci.org/tyler-johnson/rwmutex)

A simple read-write mutex/lock using JavaScript Promises. This is inspired by Golang's sync.RWMutex.

## Install

Install via NPM:

```sh
npm i rwmutex --save
```

## Usage

This package exports a RWMutex class.

```js
import RWMutex from "rwmutex";
// or
const RWMutex = require("rwmutex");
```

Create a RWMutex with the constructor. You can optionally pass in a string label for identifying the mutex in debugging. Each mutex operates independently, they will not block each other when locked.

```js
const mutex = new RWMutex();
```

To create a typical lock, that blocks all future locks, use the `.lock()` method. Typically this is used when writing data. This returns a promise that will resolve when unblocked. It returns the unlock method that must be called to unblock the mutex.

```js
function safeWrite() {
  return mutex.lock().then((unlock) => {
    // write data to database or other mutating task

    // and later
    unlock();
  });
}
```

To create a read lock, use the `.rlock()` method. This method is very similar to `.lock()`, but can be called in parallel with other calls to `.rlock()` without blocking. Calls to `.rlock()` will block future calls to `.lock()`.

```js
function safeRead() {
  return mutex.rlock().then((unlock) => {
    // read data from database or other non-mutating task

    // and later
    unlock();
  });
}
```

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