summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Noel <github@calendee.com>2013-10-28 13:46:42 -0500
committerJustin Noel <github@calendee.com>2013-10-28 13:46:42 -0500
commitf253cef6fcf8ff679b79639358b1650944603db2 (patch)
tree51e6367a9056be6d412e3f6a4a0247b62c24435e
parente345e1e29d41483d6064a8edb7645824bf5d47e2 (diff)
downloadasync-f253cef6fcf8ff679b79639358b1650944603db2.tar.gz
Added additional example for async.each
Signed-off-by: Justin Noel <github@calendee.com>
-rw-r--r--README.md35
1 files changed, 34 insertions, 1 deletions
diff --git a/README.md b/README.md
index aff0040..ae34987 100644
--- a/README.md
+++ b/README.md
@@ -168,7 +168,40 @@ __Arguments__
* callback(err) - A callback which is called after all the iterator functions
have finished, or an error has occurred.
-__Example__
+__Examples__
+
+```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');
+ }
+});
+```
+
+---------------------------------------
```js
// assuming openFiles is an array of file names and saveFile is a function