@mapbox/basic-queue

1.0.1 • Public • Published

basic-queue

Provides super-simple handling of repeated asynchronous commands.

api

var q = Queue(worker, concurrency)

  • worker is a function that this queue runs. worker will receive a callback function as the second argument. The first argument is specified when you call q.add() (see below).

  • concurrency is how many simultaneous instances of the worker are allowed to run.

q.add(object)

Tell the queue to start a new worker when there is room to do so. The object passed to .add is the first argument passed to the worker function.

events

error

When worker errors, pass an object to the callback function. This will cause the queue to emit an error event.

empty

Indicates that there is no work left in the queue.

example

var request = require('request');
var Queue = require('basic-queue');
var q = new Queue(scrape, 2);

function scrape(url, callback) {
    request(url, function(err, resp) {
        if (err) return callback(err);
        console.log(resp.body);
        callback();
    }
}

q.on('error', function(err) {
    console.error(err);
});

q.on('empty', function() {
    console.log('All done!');
})

q.add('https://www.somewhere.com');
q.add('https://www.someplace.com');
q.add('https://www.something.com');
q.add('https://www.someother.com');

Readme

Keywords

Package Sidebar

Install

npm i @mapbox/basic-queue

Weekly Downloads

189

Version

1.0.1

License

BSD

Last publish

Collaborators

  • mbx-npm-ci-production
  • mbx-npm-ci-staging
  • mbx-npm-advanced-actions-production
  • mbx-npm-advanced-actions-staging
  • mbx-npm-09-production
  • mbx-npm-08-production
  • mbx-npm-07-production
  • mbx-npm-06-production
  • mbx-npm-05-production
  • mbx-npm-04-production
  • mbx-npm-03-production
  • mbx-npm-02-production
  • mbx-npm-01-production
  • mbx-npm-02-staging
  • mapbox-npm-01
  • mapbox-npm-02
  • mapbox-npm-07
  • mapbox-npm-03
  • mapbox-npm-04
  • mapbox-npm-09
  • mapbox-npm-05
  • mapbox-npm-06
  • mapbox-npm-08
  • mapbox-npm-advanced-actions
  • mapbox-npm-ci
  • mapbox-npm
  • mapbox-admin
  • mapbox-machine-user