# async-rwlock

> Promise-based asynchronous readers-writers lock

Latest version **1.1.1** (published 2019-02-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-rwlock
pnpm add async-rwlock
yarn add async-rwlock
bun add async-rwlock
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-02-04 |
| First published | 2018-08-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 0 |
| Unpacked size | 14 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Muhamad Visat Sutarno |
| Maintainers | mvisat |
| Keywords | async, concurrency, lock, rwlock, promise |

## Links

- npm: https://www.npmjs.com/package/async-rwlock
- Repository: https://github.com/mvisat/async-rwlock
- Homepage: https://github.com/mvisat/async-rwlock#readme
- Issues: https://github.com/mvisat/async-rwlock/issues
- npm.io page: https://npm.io/package/async-rwlock

## 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.1.1 (latest) — 2019-02-04
- 1.1.0 (1.1.0) — 2019-02-04
- 1.0.0 — 2018-08-18

## README

# async-rwlock
Promise-based asynchronous readers-writers lock. Timeout is also supported.

## Install
```
$ npm install --save async-rwlock
```
or
```
$ yarn add async-rwlock
```

## Quick Usage

```js
const RWLock = require('async-rwlock').RWLock;

const lock = new RWLock();

lock.readLock()
.then(() => {
    console.log('read lock acquired');
    lock.unlock();
});

lock.writeLock()
.then(() => {
    console.log('write lock acquired');
    lock.unlock();
});
```

### Async/Await

```ts
import RWLock from 'async-rwlock';

async function testLock() {
    const lock = new RWLock();

    await lock.readLock();
    console.log('read lock acquired');
    lock.unlock();

    await lock.writeLock();
    console.log('write lock acquired');
    lock.unlock();
}

(async () => { await testLock(); })();
```

## Advanced Usage

### Timeout
```js
const RWLock = require('async-rwlock').RWLock;

const lock = new RWLock();

lock.readLock()
.then(() => {
    console.log('read lock acquired');
    // lock.unlock() // oops!
});

lock.writeLock(1000) // 1 second
.then(() => {
    console.log('write lock will never be acquired');
})
.catch((err) => {
    console.error('promise will be rejected if timeout occured');
    console.error(err);
});
```

#### Async/Await

```ts
import RWLock from 'async-rwlock';

async function testLock() {
    const lock = new RWLock();

    await lock.readLock();
    console.log('read lock acquired');
    // lock.unlock() // oops!

    try {
        await lock.writeLock(1000); // 1 second
        console.log('write lock will never be acquired');
    } catch (err) {
        console.error('error will be thrown if timeout occured');
        console.error(err);
    }
}

(async () => { await testLock(); })();
```

## API

### `new RWLock()`
- Returns: instance of `RWLock`.

#### `readLock([timeout])`
- `timeout?: number`. **Default**: `Infinity`.
- Returns: `Promise<void>`.

#### `writeLock([timeout])`
- `timeout?: number`. **Default**: `Infinity`.
- Returns: `Promise<void>`.

Acquire a read/write lock.

`timeout` is how long it will wait to acquire the lock before promise is rejected in milliseconds. If `timeout` is not in range `0 <= timeout < Infinity`, it will wait indefinitely.

#### `unlock()`
- Returns: `void`.

Release current lock. Must be called after operation using read/write lock is finished.

#### `getState()`
- Returns: `State`.

Get current state of lock. The states are either `Idle`, `Reading`, or `Writing`.

## License
[MIT](LICENSE)

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