# pending-queue

> pending-queue ensures a certain asynchronous method only run once, and queues listeners which are registered to it.

Latest version **1.2.1** (published 2017-11-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install pending-queue
pnpm add pending-queue
yarn add pending-queue
bun add pending-queue
```

## 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.2.1 |
| Published | 2017-11-27 |
| First published | 2017-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | pending-queue, promise, queue, pending |

## Links

- npm: https://www.npmjs.com/package/pending-queue
- Repository: https://github.com/kaelzhang/node-pending-queue
- Homepage: https://github.com/kaelzhang/node-pending-queue#readme
- Issues: https://github.com/kaelzhang/node-pending-queue/issues
- npm.io page: https://npm.io/package/pending-queue

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.2.1 (latest) — 2017-11-27
- 1.2.0 — 2017-03-09
- 1.1.1 — 2017-03-08
- 1.1.0 — 2017-03-08
- 1.0.0 — 2017-03-08
- 0.0.0 — 2017-03-08

## README

[![Build Status](https://travis-ci.org/kaelzhang/node-pending-queue.svg?branch=master)](https://travis-ci.org/kaelzhang/node-pending-queue)
<!-- optional appveyor tst
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/kaelzhang/node-pending-queue?branch=master&svg=true)](https://ci.appveyor.com/project/kaelzhang/node-pending-queue)
-->
<!-- optional npm version
[![NPM version](https://badge.fury.io/js/pending-queue.svg)](http://badge.fury.io/js/pending-queue)
-->
<!-- optional npm downloads
[![npm module downloads per month](http://img.shields.io/npm/dm/pending-queue.svg)](https://www.npmjs.org/package/pending-queue)
-->
<!-- optional dependency status
[![Dependency Status](https://david-dm.org/kaelzhang/node-pending-queue.svg)](https://david-dm.org/kaelzhang/node-pending-queue)
-->

# pending-queue

`pending-queue` ensures a certain asynchronous method only run once, and queues listeners which are registered to it.

## Install

```sh
$ npm install pending-queue --save
```

## Usage

```js
const Queue = require('pending-queue')

let counter = 0
const queue = new Queue({
  load: (a, b) => {
    return new Promise((resolve) => {
      counter ++
      setTimeout(() => {
        resolve(a + b)
      }, 100)
    })
  }
})

function run () {
  queue.add(1, 2).then((value) => {
    console.log(value, counter)
  })
}

run()
run()
run()

// 3, 1
// 3, 1
// 3, 1

// So the load function ran only once.
```

## new Queue({load, stringify})

- **load** `function(...params)` the method to get the value
- **stringify** `function(params)=JSON.stringify` stringify the parameters as the key to queue all asynchronous requests.

Returns `EventEmitter`, and `key` as the event name, so you can use `queue.listenerCount(key)` to see if there are pending tasks.

## Events

- **load**

## .add(...params)

- **params** `Arguments` which will be passed into `load`

Returns `Promise`

## .addWithKey(key, ...params)

- **key** `String`

Return `Promise`

Specifies the key ourself, and avoid using `options.stringify` to serialize the key from `params`.

But pay attension that there should be a consistent **one-to-one** match between `key` and `params`, or make sure that you exactly know what you are doing.

## License

MIT

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