From 5c97e91c41045caa769918fd32b6c925d9b3b014 Mon Sep 17 00:00:00 2001 From: Paris Holley Date: Thu, 9 Jun 2016 12:13:18 -0700 Subject: [PATCH 1/2] support promises in object --- .gitignore | 1 + async.coffee | 6 +++++- test.coffee | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3308354..8b1eea3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea /index.js /node_modules .*.swp diff --git a/async.coffee b/async.coffee index 06f5258..665ebe3 100644 --- a/async.coffee +++ b/async.coffee @@ -141,7 +141,11 @@ module.exports = async = ).then(-> results) parallel: Q.promised (tasks) -> - processArrayOrObject tasks, (arr) -> Q.all arr.map Q.try + processArrayOrObject tasks, (arr) -> Q.all arr.map (task) -> + if task.then + task + else + Q.try task parallelLimit: Q.promised (tasks, limit) -> processArrayOrObject tasks, (arr) -> diff --git a/test.coffee b/test.coffee index abc567d..020e542 100644 --- a/test.coffee +++ b/test.coffee @@ -265,6 +265,15 @@ describe 'parallel()', -> /^error1$/ ) + it 'accepts an object of promises', -> + obj = + one: Q.delay(125).thenResolve 1 + two: Q.delay(200).thenResolve 2 + three: Q.delay(50).thenResolve [3,3] + + async.parallel(obj).then (results) -> + deepEqual results, one: 1, two: 2, three: [3, 3] + it 'accepts an object', -> call_order = [] async.parallel(getFunctionsObject call_order).then (results) -> From e870c54fc505c8b13a4773305d896d89b57cc6bb Mon Sep 17 00:00:00 2001 From: Paris Holley Date: Thu, 9 Jun 2016 13:06:33 -0700 Subject: [PATCH 2/2] readme update --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9bcbc3a..eae914e 100644 --- a/README.md +++ b/README.md @@ -588,8 +588,8 @@ all. __Arguments__ -* tasks - An array or object containing functions to run, each function - should return a promise for an optional value. +* tasks - An array, object containing functions to run (each function + should return a promise for an optional value), or an object containing promises that resolve a value __Example__ @@ -603,7 +603,7 @@ async.parallel([ doStuff() .done() -### an example using an object instead of an array ### +### an example using an object of functions instead of an array ### async.parallel({ one: -> Q.delay(200).thenResolve(1) two: -> Q.delay(100).thenResolve(2) @@ -611,6 +611,15 @@ async.parallel({ ### results is now equals to: {one: 1, two: 2} ### doStuff() .done() + +### an example using an object of promises instead of an array ### +async.parallel({ +one: Q.delay(200).thenResolve(1) +two: Q.delay(100).thenResolve(2) +}).then (results) -> + ### results is now equals to: {one: 1, two: 2} ### + doStuff() +.done() ``` ---------------------------------------