UNPKG

@nathanfaucett/parallel

Version:

parallel for the browser and node.js

159 lines (151 loc) 3.09 kB
var tape = require("tape"), parallel = require(".."); tape("parallel(tasks: Array<Function>, callback: Function)should call array of tasks in parallel", function (assert) { parallel([ function (done) { process.nextTick(function () { done(undefined, { id: 1 }); }); }, function (done) { process.nextTick(function () { done(undefined, { id: 2 }); }); }, function (done) { process.nextTick(function () { done(undefined, { id: 3 }); }); } ], function (err, results) { assert.equal(err, undefined); assert.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }]); assert.end(); }); }); tape("should exit and call callback with error", function (assert) { parallel([ function (done) { process.nextTick(function () { done(new Error("not found")); }); }, function (done) { process.nextTick(function () { done(undefined, { id: 2 }); }); } ], function (err) { assert.equal(err.message, "not found"); assert.end(); }); }); tape("should throw an error if a task is not a function", function (assert) { try { parallel([ "string" ], function () {}); } catch (err) { assert.equal(err.message, "tasks must be functions"); assert.end(); } }); tape("parallel(tasks: Object<Function>, callback: Function) should call object tasks in parallel", function (assert) { parallel({ "first": function (done) { process.nextTick(function () { done(undefined, { id: 1 }); }); }, "second": function (done) { process.nextTick(function () { done(undefined, { id: 2 }); }); }, "last": function (done) { process.nextTick(function () { done(undefined, { id: 3 }); }); } }, function (err, results) { assert.equal(err, undefined); assert.deepEqual(results, { "first": { id: 1 }, "second": { id: 2 }, "last": { id: 3 } }); assert.end(); }); }); tape("parallel(tasks: Object<Function>, callback: Function) should exit and call callback with error", function (assert) { parallel({ "first": function (done) { process.nextTick(function () { done(new Error("not found")); }); }, "last": function (done) { process.nextTick(function () { done(undefined, { id: 2 }); }); } }, function (err) { assert.equal(err.message, "not found"); assert.end(); }); }); tape("parallel(tasks: Object<Function>, callback: Function) should throw an error if a task is not a function", function (assert) { try { parallel({ "first": "string", "second": function (done) { done(); } }, function () {}); } catch (err) { assert.equal(err.message, "tasks must be functions"); assert.end(); } }); tape("parallel.from(... tasks: Function) -> Function should create function from arguments", function (assert) { var f = parallel.from( function one(done) { done(undefined, 0); }, function two(done) { done(undefined, 1); } ); f(function (error, results) { assert.deepEqual(results, [0, 1]); assert.end(); }); });