summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolan McMahon <caolan.mcmahon@gmail.com>2014-03-28 15:04:17 +0000
committerCaolan McMahon <caolan.mcmahon@gmail.com>2014-03-28 15:04:17 +0000
commit1961d7da85cc9d13e97e3c56cdac98babc89da80 (patch)
tree7ce531fa3007fdf9db94760ab3d0112c1ba2526f
parent1513f799ddf351473d01172626eaa4935e11cfa1 (diff)
parentb6408da24722c021e7529c8a8314b32991d445bd (diff)
downloadasync-1961d7da85cc9d13e97e3c56cdac98babc89da80.tar.gz
Merge pull request #401 from calendee/eachDocumentation
Additional Example for async.each
-rw-r--r--README.md34
1 files changed, 33 insertions, 1 deletions
diff --git a/README.md b/README.md
index 8f4a1c8..a41aebd 100644
--- a/README.md
+++ b/README.md
@@ -182,7 +182,8 @@ __Arguments__
* `callback(err)` - A callback which is called when all `iterator` functions
have finished, or an error occurs.
-__Example__
+__Examples__
+
```js
// assuming openFiles is an array of file names and saveFile is a function
@@ -193,6 +194,37 @@ async.each(openFiles, saveFile, function(err){
});
```
+```js
+// assuming openFiles is an array of file names and saveFile is a function
+// to save the modified contents of that file:
+
+async.each(openFiles, function( file, callback) {
+
+ // Perform operation on file here.
+ console.log('Processing file ' + file);
+ callback();
+
+ if( file.length > 32 ) {
+ console.log('This file name is too long');
+ callback('File name too long');
+
+ return;
+ } else {
+ console.log('File saved');
+ callback();
+ }
+}, function(err){
+ // if any of the saves produced an error, err would equal that error
+ if( err ) {
+ // One of the iterations produced an error.
+ // All processing will now stop.
+ console.log('A file failed to process');
+ } else {
+ console.log('All files have been processed successfully');
+ }
+});
+```
+
---------------------------------------
<a name="forEachSeries" />