Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kaelzhang/node-pending-queue
pending-queue ensures a certain asynchronous method only run once, and queues listeners which are registered to it.
https://github.com/kaelzhang/node-pending-queue
asynchronous nodejs promise queue
Last synced: 24 days ago
JSON representation
pending-queue ensures a certain asynchronous method only run once, and queues listeners which are registered to it.
- Host: GitHub
- URL: https://github.com/kaelzhang/node-pending-queue
- Owner: kaelzhang
- License: other
- Created: 2017-03-08T06:22:38.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-27T06:00:50.000Z (about 7 years ago)
- Last Synced: 2024-12-06T05:16:29.202Z (about 1 month ago)
- Topics: asynchronous, nodejs, promise, queue
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/kaelzhang/node-pending-queue.svg?branch=master)](https://travis-ci.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