From f42ad6ef775aa9553aeb42b9d25147f0e94b32be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Aldana?= Date: Thu, 30 Jun 2016 01:52:11 -0500 Subject: First commit, reflectAll allow to accept object of functions --- lib/reflectAll.js | 13 +++++++- mocha_test/parallel.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/lib/reflectAll.js b/lib/reflectAll.js index ac9ea06..d1ad247 100644 --- a/lib/reflectAll.js +++ b/lib/reflectAll.js @@ -1,4 +1,5 @@ import reflect from './reflect'; +import isArray from 'lodash/isArray'; /** * A helper function that wraps an array of functions with reflect. @@ -40,5 +41,15 @@ import reflect from './reflect'; * }); */ export default function reflectAll(tasks) { - return tasks.map(reflect); + var results; + if (isArray(tasks)) { + results = tasks.map(reflect); + } else { + var keys = Object.keys(tasks); + results = {}; + keys.forEach(function(key) { + results[key] = reflect.call(this, tasks[key]) + }); + } + return results; } diff --git a/mocha_test/parallel.js b/mocha_test/parallel.js index 5362491..fe99676 100644 --- a/mocha_test/parallel.js +++ b/mocha_test/parallel.js @@ -4,7 +4,7 @@ var assert = require('assert'); var isBrowser = require('./support/is_browser'); var getFunctionsObject = require('./support/get_function_object'); -describe('parallel', function() { +describe.only('parallel', function() { it('parallel', function(done) { var call_order = []; @@ -241,6 +241,88 @@ describe('parallel', function() { }); }); + it('parallel object with reflect all (values and errors)', function(done) { + var tasks = { + one: function(callback) { + setTimeout(function() { + callback(null, 'one'); + }, 200); + }, + two: function(callback) { + callback('two'); + }, + three: function(callback) { + setTimeout(function() { + callback(null, 'three'); + }, 100); + } + }; + + async.parallel(async.reflectAll(tasks), function(err, results) { + expect(results).to.eql({ + one: { value: 'one' }, + two: { error: 'two' }, + three: { value: 'three' } + }); + done(); + }) + }); + + it('parallel empty object with reflect all', function(done) { + var tasks = {}; + + async.parallel(async.reflectAll(tasks), function(err, results) { + expect(results).to.eql({}); + done(); + }) + }); + + it('parallel empty object with reflect all (errors)', function(done) { + var tasks = { + one: function(callback) { + callback('one'); + }, + two: function(callback) { + callback('two'); + }, + three: function(callback) { + callback('three'); + } + }; + + async.parallel(async.reflectAll(tasks), function(err, results) { + expect(results).to.eql({ + one: { error: 'one' }, + two: { error: 'two' }, + three: { error: 'three' } + }); + done(); + }) + }); + + it('parallel empty object with reflect all (values)', function(done) { + var tasks = { + one: function(callback) { + callback(null, 'one'); + }, + two: function(callback) { + callback(null, 'two'); + }, + three: function(callback) { + callback(null, 'three'); + } + }; + + async.parallel(async.reflectAll(tasks), function(err, results) { + expect(results).to.eql({ + one: { value: 'one' }, + two: { value: 'two' }, + three: { value: 'three' } + }); + done(); + }) + }); + it('parallel does not continue replenishing after error', function(done) { var started = 0; var arr = [ -- cgit v1.2.1 From 2564a54efc7bd97cf4ff0b8629ecda66741a1142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Aldana?= Date: Thu, 30 Jun 2016 01:59:33 -0500 Subject: Remove only from parallel test --- mocha_test/parallel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mocha_test/parallel.js b/mocha_test/parallel.js index fe99676..bad98af 100644 --- a/mocha_test/parallel.js +++ b/mocha_test/parallel.js @@ -4,7 +4,7 @@ var assert = require('assert'); var isBrowser = require('./support/is_browser'); var getFunctionsObject = require('./support/get_function_object'); -describe.only('parallel', function() { +describe('parallel', function() { it('parallel', function(done) { var call_order = []; -- cgit v1.2.1 From 3b5b1505778ccaf30fbd933b248a0906d1fdd97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Aldana?= Date: Thu, 30 Jun 2016 22:34:38 -0500 Subject: Add object of tasks documentation --- lib/reflectAll.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/reflectAll.js b/lib/reflectAll.js index d1ad247..29d6106 100644 --- a/lib/reflectAll.js +++ b/lib/reflectAll.js @@ -2,7 +2,7 @@ import reflect from './reflect'; import isArray from 'lodash/isArray'; /** - * A helper function that wraps an array of functions with reflect. + * A helper function that wraps an array or an object of functions with reflect. * * @name reflectAll * @static @@ -39,6 +39,32 @@ import isArray from 'lodash/isArray'; * // results[1].error = Error('bad stuff happened') * // results[2].value = 'two' * }); + * + * // an example using an object instead of an array + * let tasks = { + * one: function(callback) { + * setTimeout(function() { + * callback(null, 'one'); + * }, 200); + * }, + * two: function(callback) { + * callback('two'); + * }, + * three: function(callback) { + * setTimeout(function() { + * callback(null, 'three'); + * }, 100); + * } + * }; + * + * async.parallel(async.reflectAll(tasks), + * // optional callback + * function(err, results) { + * // values + * // results.one.value = 'one' + * // results.two.error = 'two' + * // results.three.value = 'three' + * }); */ export default function reflectAll(tasks) { var results; @@ -48,7 +74,7 @@ export default function reflectAll(tasks) { var keys = Object.keys(tasks); results = {}; keys.forEach(function(key) { - results[key] = reflect.call(this, tasks[key]) + results[key] = reflect.call(this, tasks[key]); }); } return results; -- cgit v1.2.1