[Pkg-javascript-commits] [node-async] 40/480: added the concat function
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 263f71f52d6f8d0b73d47223c6e761c06de90b21
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Thu Sep 16 10:21:46 2010 +0100
added the concat function
---
README.md | 32 ++++++++++++++++++++++++++++----
lib/async.js | 12 ++++++++++++
test/test-async.js | 28 ++++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 0ea8e8b..eae3cd6 100644
--- a/README.md
+++ b/README.md
@@ -99,6 +99,9 @@ some
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.
+
### Flow Control
@@ -268,7 +271,7 @@ reducing it then its probably a good idea to do so.
__Arguments__
-* arr - An array to iterator over.
+* arr - An array to iterate over.
* memo - The initial state of the reduction.
* iterator(memo, item, callback) - A function applied to each item in the
array to produce the next step in the reduction. The iterator is passed a
@@ -308,7 +311,7 @@ If order within the original array is important then look at detectSeries.
__Arguments__
-* arr - An array to iterator over.
+* arr - An array to iterate over.
* iterator(item, callback) - A truth test to apply to each item in the array.
The iterator is passed a callback which must be called once it has completed.
* callback(result) - A callback which is called as soon as any iterator returns
@@ -367,7 +370,7 @@ call returns true, the main callback is immediately called.
__Arguments__
-* arr - An array to iterator over.
+* arr - An array to iterate over.
* iterator(item, callback) - A truth test to apply to each item in the array.
The iterator is passed a callback which must be called once it has completed.
* callback(result) - A callback which is called as soon as any iterator returns
@@ -391,7 +394,7 @@ way node libraries work with truth tests like path.exists.
__Arguments__
-* arr - An array to iterator over.
+* arr - An array to iterate over.
* iterator(item, callback) - A truth test to apply to each item in the array.
The iterator is passed a callback which must be called once it has completed.
* callback(result) - A callback which is called after all the iterator
@@ -404,6 +407,27 @@ __Example__
// if result is true then every file exists
});
+### concat(arr, iterator, callback)
+
+Applies an iterator to each item in a list, concatenating the results. Returns the
+concatenated list.
+
+__Arguments__
+
+* arr - An array to iterate over
+* iterator(item, callback) - A function to apply to each item in the array.
+ The iterator is passed a callback which must be called once it has completed
+ with an error (which can be null) and an array of results.
+* callback(err, results) - A callback which is called after all the iterator
+ functions have finished, or an error has occurred. Results is an array containing
+ the concatenated results of the iterator function.
+
+__Example__
+
+ async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files){
+ // files is now a list of filenames that exist in the 3 directories
+ });
+
### series(tasks, [callback])
diff --git a/lib/async.js b/lib/async.js
index 24e6279..a20211e 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -396,6 +396,18 @@
};
};
+ async.concat = function(arr, fn, callback){
+ var r = [];
+ async.forEach(arr, function(x, cb){
+ fn(x, function(err, y){
+ r = r.concat(y || []);
+ cb(err);
+ });
+ }, function(err){
+ callback(err, r);
+ });
+ };
+
var _console_fn = function(name){
return function(fn){
var args = Array.prototype.slice.call(arguments, 1);
diff --git a/test/test-async.js b/test/test-async.js
index de03d88..4175145 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -872,3 +872,31 @@ exports['noConflict'] = function(test){
test.done();
});
};
+
+exports['concat'] = function(test){
+ var iterator = function (x, cb) {
+ var r = [];
+ while (x > 0) {
+ r.push(x);
+ x--;
+ }
+ process.nextTick(function(){
+ cb(null, r);
+ });
+ };
+ async.concat([1,2,3], iterator, function(err, results){
+ test.same(results, [1,2,1,3,2,1]);
+ test.ok(!err);
+ test.done();
+ });
+};
+
+exports['concat error'] = function(test){
+ var iterator = function (x, cb) {
+ cb(new Error('test error'));
+ };
+ async.concat([1,2,3], iterator, function(err, results){
+ 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