domain-async

run things concurrently in domains

Domain-Async

One of the greatest modules oit there is async by Caolan McMahon. I used to use it every day for jsut about everything. While it's great, I did look at the code. It made my eyes hurt. That is probably more due to my sensitive eyes than any failing on Caolan's part, but they still hurt. The other thing is that it's a library, that does just way too much for my tastes. The only functions I really used were series and parallel. Then Node.JS went up to version 0.8 and Domains came along. Domains are absolutely awesome for error handling, and I have fallen in love with them.

So was born the idea for Domain-Async, which tries to be a simple base for doing stuff in series or parallel and have all the tasks each have their own domain. At the same time, I wanted it to be API compatible with async.series and async.parallel.

var async = require('domain-async');
tasks = [];

async(tasks).done(doneCallback);

function doneCallback(errs, vals) {
  // Go forth and be merry
}

The basic async is a function which accepts 0 or more tasks to execute (arrays of tasks are OK too). It then return an object with the following methods:

  1. obj.then(task) // adds another task (or array of tasks to be executed)
  2. obj.throttle(count) // limits the maximum number of tasks to run concurrently (default: Number.MAX_VALUE)
  3. obj.progress(function callback(done, running, waiting, total) {}) // provides a way to get notified about progress
  4. obj.done(function callback(errs, vals) {}) // starts the processing and calls callback with the results
  5. obj.values(function callback(vals) {}) // basically the same as done except it ignores errors and returns the array of values
  6. obj.errorStop(function(err, val) {}) // stops on the first error. If it get's through all tasks returns the last value

In order to be API compatible with async, there are 2 functions on the basic module.

  1. async.series(tasks, callback)
  2. async.parallel(tasks, callback)

This is implemented as:

function series(tasks, callback) {
  tasks = concurrent(tasks)
  tasks = tasks.throttle(1);
  tasks.errorStop(callback);
}

This is implemented as:

function parallel(tasks, callback) {
  tasks = concurrent(tasks);
  tasks.done(callback);
}