0.1.1 • Published 9 years ago

cargo-ship v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

cargo-ship

Parallel execution of tasks with a shared namespace

npm version Travis Coveralls

The extremly well-known parallel execution of tasks, but with a cargo, a shared object where tasks can store data. It's like a cargo ship, cranes (tasks) storing cargo (data). Each task writing to the shared object.

It's very useful when you need to call a bunch of functions in parallel and store the results in a common place.

var cranes = [
  function (cargo, done) {
    cargo.a = 1;
    done();
  },
  function (cargo, done) {
    cargo.b = 2;
    done();
  },
  function (cargo, done) {
    cargo.c = 3;
    done();
  }
];

ship.load(cranes, function (err, cargo) {
  // cargo { a: 1, b: 2, c: 3 }
});

It's basically the same behaviour as the async.parallel() but with a sightly! and slightly different interface.

module.load(cranes, cargo, callback) : undefined

Executes all tasks in parallel.

cranes is an array of functions to run in parallel. Each function has the signature function(cargo, done), where cargo is the shared object and done the function to call when the task finishes. As usual, pass an error to done() to abort the execution of the tasks. This is the error returned by the load() function. Because aborting asynchronous parallel tasks is not possible once they begin, the callback is guaranteed to be called only once with the first error occurred.

A cargo can be passed from outside. Use the second parameter to initialize the cargo with data.

var cranes = [
  function (cargo, done) {
    cargo.b = 2;
    done();
  },
  function (cargo, done) {
    cargo.c = 3;
    done();
  }
];

ship.load(cranes, { a: 1 }, function (err, cargo) {
  // cargo { a: 1, b: 2, c: 3 }
});