pending-queue
pending-queue
ensures a certain asynchronous method only run once, and queues listeners which are registered to it.
Install
$ npm install pending-queue --save
Usage
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()
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.
add(...params)
- params
Arguments
which will be passed into load
Returns Promise
License
MIT