# mutexify

> mutex lock for javascript

Latest version **1.4.0** (published 2021-11-29) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2021-11-29 |
| First published | 2014-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/mutexify) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 89 |
| Author | Mathias Buus |
| Maintainers | mafintosh |
| Keywords | mutex, lock |

## Links

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

## Dependencies (1)

- [queue-tick](https://npm.io/package/queue-tick.md) ^1.0.0

## Alternatives

- [@oh-my-pi/pi-natives](https://npm.io/package/@oh-my-pi/pi-natives.md) — 51.8K weekly downloads
- [@capgo/capacitor-light-sensor](https://npm.io/package/@capgo/capacitor-light-sensor.md) — 3.0K weekly downloads
- [@heyhuynhgiabuu/pi-diff](https://npm.io/package/@heyhuynhgiabuu/pi-diff.md) — 492 weekly downloads
- [@lotsa/verdant-lang-asm](https://npm.io/package/@lotsa/verdant-lang-asm.md) — 38 weekly downloads
- [new-era-syntax](https://npm.io/package/new-era-syntax.md) — 20 weekly downloads

## Recent versions

- 1.4.0 (latest) — 2021-11-29
- 1.3.1 — 2020-09-01
- 1.3.0 — 2020-04-29
- 1.2.0 — 2017-08-10
- 1.1.0 — 2015-05-14
- 1.0.1 — 2014-11-10
- 1.0.0 — 2014-11-10

## README

# mutexify

Bike shed mutex lock implementation in node.js

```
npm install mutexify
```

[![build status](http://img.shields.io/travis/mafintosh/mutexify.svg?style=flat)](http://travis-ci.org/mafintosh/mutexify)

Hasn't this been done before? Yes, but the specific semantics of this made some of my code simpler.

## Usage


``` js
var mutexify = require('mutexify')
var lock = mutexify()

lock(function(release) {
  console.log('i am now locked')
  setTimeout(function() {
    release()
  }, 1000)
})

lock(function(release) {
  console.log('1 second later')
  release()
})
```

A common pattern is to call a callback after you release the lock.
To do this in a one-liner pass the callback and the value to `release(cb, err, value)`

``` js
var write = function(data, cb) {
  lock(function(release) {
    fs.writeFile('locked-file.txt', data, release.bind(null, cb))
  })
}
```

`mutexify` guarantees that the order that a mutex was requested in is the order that access will be given.

You can read the lock's current state on the `lock.locked` property.

### Usage with promises

`mutexify` provides a Promise-based alternative.

```js
const mutexify = require('mutexify/promise')

;(async () => {
  var lock = mutexify()

  var release = await lock()
  console.log('i am now locked')
  setTimeout(function () {
    release()
  }, 1000)

  release = await lock()
  console.log('1 second later')
  release()
})()
```

## License

MIT

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