JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3934
  • Score
    100M100P100Q145195F
  • License MIT

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

Package Exports

  • pending-queue

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pending-queue) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status

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()

// 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