# hashqueue

> Serializes task execution based on ids

Latest version **0.1.1** (published 2015-11-30) · MIT License - http://opensource.org/licenses/MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2015-11-30 |
| First published | 2015-03-18 |
| Weekly downloads | 0 |
| License | MIT License - http://opensource.org/licenses/MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | LeanKit |
| Maintainers | arobson |
| Keywords | mutual exclusion |

## Links

- npm: https://www.npmjs.com/package/hashqueue
- Repository: https://github.com/LeanKit-Labs/hashqueue
- Issues: https://github.com/LeanKit-Labs/hashqueue/issues
- npm.io page: https://npm.io/package/hashqueue

## Dependencies (2)

- [when](https://npm.io/package/when.md) ^3.7.2
- [haberdasher](https://npm.io/package/haberdasher.md) ~0.2.0

## Recent versions

- 0.1.1 (latest) — 2015-11-30
- 0.1.0 — 2015-03-18

## README

## hashqueue
A task queue that serializes tasks based on id. Guarantees that no two tasks assigned the same id can execute concurrently.

Recommended use for limiting concurrent mutation of state within a service boundary.

## API

__Example__
```javascript
var hashqueue = require( 'hashqueue' );
var tasks = hashqueue.create( 2 );

when.all( [
	tasks.add( 1, function() { console.log( 1 ); } ),
	tasks.add( 2, function() { console.log( 2 ); } ),
	tasks.add( 3, function() { console.log( 3 ); } ),
	tasks.add( 4, function() { console.log( 4 ); } )
] )
.then( function( results ) {
	// results will be empty since the tasks aren't returning anything
} );
```

### create( concurrencyLimit )

Creates a new hashqueue with the specified concurrency limit. The default limit is 4. Higher limits are better as the set of ids increase. Long-running tasks can slow things down, especially with lower limits.

```javascript
var queue = hashqueue.create( 2 );
```

### add( id, task )
Add a task to the queue. Returns a promise that will resolve to the value returned from the task when the task has completed.

> IMPORTANT: A promise __must__ be returned from tasks that are asynchronous.

```javascript
// this example will print 'tada' after roughly 100 ms.
var promise = queue.add( 100, function() {
	return when.promise( function( resolve ) {
		setTimeout( function() {
			resolve( 'tada' );
		}, 100 );
	} );
} );

promise.then( console.log );
```

### stop()
Stops all task runners. Ideally only used before disposal of the queue; a use case that will generally come up in testing.

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