queue.ls

Promise based task queue with concurrency limit


Keywords
queue, concurrency, promise
License
Unlicense
Install
npm install queue.ls@0.2.0

Documentation

queue.ls

Promise based task queue with concurrency limit.

Example

const https = require('https');
const url = require('url');
const queue = require('queue.ls');

q = queue({ concurrency: 3 });

const tasks = [];
for (let i = 1; i <= 20; i++) {
  tasks.push(q.add(fetchPage(i)));
}
Promise.all(tasks).then(pages =>
  console.log(pages.reduce((a, b) => a.concat(b)))
);

function fetchPage(i) {
  const options = url.parse(
    `https://api.github.com/repos/jquery/jquery/commits?page=${i}`);
  options.headers = { 'User-Agent': 'Awesome-Octocat-App' };
  return function() {
    return new Promise(resolve => {
      https.get(options, res => {
        const buffer = [];
        res.on('data', chunk => buffer.push(chunk));
        res.on('end', () => resolve(JSON.parse(buffer.join(''))));
      });
    });
  };
};

Example

require! https
require! url
queue = require \queue.ls

q = queue concurrency: 3
fetch-page = (i) ->
  options = url.parse \
  "https://api.github.com/repos/jquery/jquery/commits?page=#i"
  options <<< headers: 'User-Agent': \Awesome-Octocat-App
  ->
    new Promise (resolve) ->
      res <- https.get options
      buffer = []
      res.on \data buffer~push
      <- res.on \end
      resolve JSON.parse buffer.join ''

tasks = for i from 1 to 20
  q.add fetch-page i

Promise.all tasks .then (pages) ->
  console.log pages.reduce (++)

Install

npm i --save queue.ls

queue([options])

  • options <Integer> | <Object>
    • concurrency: Maximum number of tasks should run concurrently, defaults to 1.
    • promise: your Promise class, defaults to global Promise.

Returns a queue object.

If options is an integer, then it specifies the concurrency.

queue.add(task[, callback])

  • task <Function>
  • callback <Function>

Returns a promise, resolves to the result of task.

If task does not return a Promise, the result will be wrapped by Promise.resolve.

When the result of task resolved , optional callback will be called with it.

queue.push(task[, callback])

Alias for queue.add(task, callback)

Queue Events

The queue object is an instance of EventEmitter.