Giter VIP home page Giter VIP logo

node-pending-queue's Introduction

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

node-pending-queue's People

Contributors

kaelzhang avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.