diff options
author | Alex Early <alexander.early@gmail.com> | 2016-06-30 21:34:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-30 21:34:18 -0700 |
commit | 208a29e7ab375ab4f2882b38d184f9ac655b913b (patch) | |
tree | 474ac57f19a7f68839f1b769b499440cd836ff2c | |
parent | 275b9cb8a6db8bdfaa118a4d88d33bf8b7dfc02d (diff) | |
parent | 3b5b1505778ccaf30fbd933b248a0906d1fdd97d (diff) | |
download | async-208a29e7ab375ab4f2882b38d184f9ac655b913b.tar.gz |
Merge pull request #1207 from andalm/reflectall-accept-object
ReflectAll allow to accept object of functions
-rw-r--r-- | lib/reflectAll.js | 41 | ||||
-rw-r--r-- | mocha_test/parallel.js | 82 |
2 files changed, 121 insertions, 2 deletions
diff --git a/lib/reflectAll.js b/lib/reflectAll.js index a9fe48f..4492d1d 100644 --- a/lib/reflectAll.js +++ b/lib/reflectAll.js @@ -1,7 +1,8 @@ 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,7 +40,43 @@ import reflect from './reflect'; * // 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) { - 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..bad98af 100644 --- a/mocha_test/parallel.js +++ b/mocha_test/parallel.js @@ -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 = [ |