# semaphore

> semaphore for node

Latest version **1.1.0** (published 2017-08-08) · 0 weekly downloads

## Install

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

## 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.1.0 |
| Published | 2017-08-08 |
| First published | 2012-05-25 |
| Weekly downloads | 0 |
| TypeScript types | separate (@types/semaphore) |
| Module format | CommonJS |
| Node | >=0.8.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 118 |
| Maintainers | addaleax, abrkn |

## Links

- npm: https://www.npmjs.com/package/semaphore
- Repository: https://github.com/abrkn/semaphore.js
- Issues: https://github.com/abrkn/semaphore.js/issues
- npm.io page: https://npm.io/package/semaphore

## Recent versions

- 1.1.0 (latest) — 2017-08-08
- 1.0.5 — 2016-02-27
- 1.0.4 — 2016-02-18
- 1.0.3 — 2015-06-11
- 1.0.2 — 2015-04-01
- 1.0.1 — 2012-11-05
- 1.0.0 — 2012-05-25

## README

semaphore.js
============

[![Build Status](https://travis-ci.org/abrkn/semaphore.js.svg?branch=master)](https://travis-ci.org/abrkn/semaphore.js)

Install:
npm install semaphore

Limit simultaneous access to a resource.

```javascript
// Create
var sem = require('semaphore')(capacity);

// Take
sem.take(fn[, n=1])
sem.take(n, fn)

// Leave
sem.leave([n])

// Available
sem.available([n])
```


```javascript
// Limit concurrent db access
var sem = require('semaphore')(1);
var server = require('http').createServer(req, res) {
	sem.take(function() {
		expensive_database_operation(function(err, res) {
			sem.leave();

			if (err) return res.end("Error");

			return res.end(res);
		});
	});
});
```

```javascript
// 2 clients at a time
var sem = require('semaphore')(2);
var server = require('http').createServer(req, res) {
	res.write("Then good day, madam!");

	sem.take(function() {
		res.end("We hope to see you soon for tea.");
		sem.leave();
	});
});
```

```javascript
// Rate limit
var sem = require('semaphore')(10);
var server = require('http').createServer(req, res) {
	sem.take(function() {
		res.end(".");
		
		setTimeout(sem.leave, 500)
	});
});
```

License
===

MIT

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