summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrés Aldana <andres.aldana.martinez@gmail.com>2016-06-30 01:52:11 -0500
committerAndrés Aldana <andres.aldana.martinez@gmail.com>2016-06-30 01:58:15 -0500
commitf42ad6ef775aa9553aeb42b9d25147f0e94b32be (patch)
tree84fe924ec43f7f3ce19bd9b2754fcf676dc18a51
parentb7081631c373bf459f1108824f870c62328ed8d6 (diff)
downloadasync-f42ad6ef775aa9553aeb42b9d25147f0e94b32be.tar.gz
First commit, reflectAll allow to accept object of functions
-rw-r--r--lib/reflectAll.js13
-rw-r--r--mocha_test/parallel.js84
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 = [