[Pkg-javascript-commits] [node-async] 41/480: added concatSeries
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:10 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-async.
commit 28fed845b118cf6f159c37479949fb81d76ad1e9
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Fri Sep 17 18:02:31 2010 +0100
added concatSeries
---
README.md | 11 +++++++++--
lib/async.js | 6 ++++--
test/test-async.js | 40 ++++++++++++++++++++++++++++++++--------
3 files changed, 45 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
index eae3cd6..852a64e 100644
--- a/README.md
+++ b/README.md
@@ -100,7 +100,8 @@ every
: Returns true if every element in the array satisfies an async test.
concat
-: Apply an iterator to each item in a list and concatenate the results.
+: Apply an iterator to each item in a list and concatenate the results. Series
+ version: concatSeries.
### Flow Control
@@ -410,7 +411,9 @@ __Example__
### concat(arr, iterator, callback)
Applies an iterator to each item in a list, concatenating the results. Returns the
-concatenated list.
+concatenated list. The iterators are called in parallel, and the results are
+concatenated as they return. There is no guarantee that the results array will
+be returned in the original order of the arguments passed to the iterator function.
__Arguments__
@@ -428,6 +431,10 @@ __Example__
// files is now a list of filenames that exist in the 3 directories
});
+### concatSeries(arr, iterator, callback)
+
+Same as async.concat, but executes in series instead of parallel.
+
### series(tasks, [callback])
diff --git a/lib/async.js b/lib/async.js
index a20211e..9ab14f9 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -396,9 +396,9 @@
};
};
- async.concat = function(arr, fn, callback){
+ var _concat = function(eachfn, arr, fn, callback){
var r = [];
- async.forEach(arr, function(x, cb){
+ eachfn(arr, function(x, cb){
fn(x, function(err, y){
r = r.concat(y || []);
cb(err);
@@ -407,6 +407,8 @@
callback(err, r);
});
};
+ async.concat = doParallel(_concat);
+ async.concatSeries = doSeries(_concat);
var _console_fn = function(name){
return function(fn){
diff --git a/test/test-async.js b/test/test-async.js
index 4175145..6f0f7b0 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -874,18 +874,21 @@ exports['noConflict'] = function(test){
};
exports['concat'] = function(test){
+ var call_order = [];
var iterator = function (x, cb) {
- var r = [];
- while (x > 0) {
- r.push(x);
- x--;
- }
- process.nextTick(function(){
+ setTimeout(function(){
+ call_order.push(x);
+ var r = [];
+ while (x > 0) {
+ r.push(x);
+ x--;
+ }
cb(null, r);
- });
+ }, x*25);
};
- async.concat([1,2,3], iterator, function(err, results){
+ async.concat([1,3,2], iterator, function(err, results){
test.same(results, [1,2,1,3,2,1]);
+ test.same(call_order, [1,2,3]);
test.ok(!err);
test.done();
});
@@ -900,3 +903,24 @@ exports['concat error'] = function(test){
test.done();
});
};
+
+exports['concatSeries'] = function(test){
+ var call_order = [];
+ var iterator = function (x, cb) {
+ setTimeout(function(){
+ call_order.push(x);
+ var r = [];
+ while (x > 0) {
+ r.push(x);
+ x--;
+ }
+ cb(null, r);
+ }, x*25);
+ };
+ async.concatSeries([1,3,2], iterator, function(err, results){
+ test.same(results, [1,3,2,1,2,1]);
+ test.same(call_order, [1,3,2]);
+ test.ok(!err);
+ test.done();
+ });
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git
More information about the Pkg-javascript-commits
mailing list