diff options
author | Justin Noel <github@calendee.com> | 2013-10-28 13:46:42 -0500 |
---|---|---|
committer | Justin Noel <github@calendee.com> | 2013-10-28 13:46:42 -0500 |
commit | f253cef6fcf8ff679b79639358b1650944603db2 (patch) | |
tree | 51e6367a9056be6d412e3f6a4a0247b62c24435e | |
parent | e345e1e29d41483d6064a8edb7645824bf5d47e2 (diff) | |
download | async-f253cef6fcf8ff679b79639358b1650944603db2.tar.gz |
Added additional example for async.each
Signed-off-by: Justin Noel <github@calendee.com>
-rw-r--r-- | README.md | 35 |
1 files changed, 34 insertions, 1 deletions
@@ -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 |