An lib to periodically flush a collection with a limited size based on async.cargo

Downloads in past


0.0.39 years ago9 years agoMinified + gzip package size for async-timed-cargo in KB


Async Timed Cargo
Build Status
async.cargo based task execution.

Differences from async.cargo

  • Payload number is used as a limit to the message, but most times the callback is called with a fewer number of items
  • It tries to process tasks as soon as possible when they are pushed, which is bad if used to control batch messages processing, as it will result in more calls with fewer items

  • Adds a timer concept to the task execution
  • Payload tries to be respected, if number of messages received is less than payload it does not do anything until timer is triggered or more messages are received, whatever comes first
  • It is better for message batch process as it waits until the queue is full


add async-timed-cargo to you package.json


var asyncTimedCargo = require('async-timed-cargo');

var cargo = asyncTimedCargo(function(tasks, callback) {

  // the tasks array will be [1, 2, 3]
  // this will be called after 1000ms, as number of items (3) is smaller than payload (10)
  callback('err', 'arg');

}, 10, 1000);

asyncTimedCargo.push(3, function(err, arg) {
  // err will be 'err'
  // arg will be 'arg'

Also, check the example app for a working example on how to use the middleware


async-timed-cargo(worker, payload, timeout)

  • worker(tasks, callback) - An asynchronous function for processing an array of
queued tasks, which must call its callback(err, arg) argument when finished, with an optional err and arg argument.
  • payload - An optional integer for determining how many tasks should be
processed per round; if omitted, the default is unlimited and only timer will be considered
  • timeout - An optional integer for determining the interval (in ms) to be used to flush data each round; if omitted, the default is 1000ms

Cargo objects
The cargo object returned by this function has the following properties and methods:
  • length() - A function returning the number of items waiting to be processed.
  • push(task, [callback]) - Adds task to the queue. The callback is called
once the worker has finished processing the task. Instead of a single task, an array of tasks can be submitted. The respective callback is used for every task in the list.


$ npm install
$ npm test