queue

queue (GitHub: jessetane/queue, License: MIT) by Jesse Tane is a 130 line implementation of asynchronous function queue with adjustable concurrency. This allows your to schedule a bunch of async jobs and throttle them for whatever reason. This is pretty useful when you want to avoid overwhelming the system, be that a remote host or a local CPU.

 

npm install queue

Usage

queue exports a class Queue that implements most of the Array API. Pass async functions (ones that accept a callback) to an instance’s additive array methods. Processing begins when you call start().

In the example below we have two queues, each with 20 jobs that take 500ms each. First queue fires all of them at the same time. Second queue fires two at a time.

var queue = require('queue');

function populateQueue(q) {
  for (var i = 0; i < 20; i++) {
    (function(index) {
      q.push(function(done) {
        console.log('done', index);
        setTimeout(done, 500);
      });
    })(i);
  }
}

var q1 = queue();
var q2 = queue({concurrency: 2});

populateQueue(q1);
populateQueue(q2);

q1.start();
q2.start();

What Else?

Checkout the runnable example and github example repository. Are you trying to limit your concurent load? Would love to hear why!

Post navigation

ware

ndm

Leave a comment

Leave a Reply

Your email address will not be published.