diff options
author | Caolan McMahon <caolan.mcmahon@gmail.com> | 2014-03-28 15:04:17 +0000 |
---|---|---|
committer | Caolan McMahon <caolan.mcmahon@gmail.com> | 2014-03-28 15:04:17 +0000 |
commit | 1961d7da85cc9d13e97e3c56cdac98babc89da80 (patch) | |
tree | 7ce531fa3007fdf9db94760ab3d0112c1ba2526f /README.md | |
parent | 1513f799ddf351473d01172626eaa4935e11cfa1 (diff) | |
parent | b6408da24722c021e7529c8a8314b32991d445bd (diff) | |
download | async-1961d7da85cc9d13e97e3c56cdac98babc89da80.tar.gz |
Merge pull request #401 from calendee/eachDocumentation
Additional Example for async.each
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 34 |
1 files changed, 33 insertions, 1 deletions
@@ -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" /> |