diff options
Diffstat (limited to 'docs/v3/each.js.html')
-rw-r--r-- | docs/v3/each.js.html | 89 |
1 files changed, 65 insertions, 24 deletions
diff --git a/docs/v3/each.js.html b/docs/v3/each.js.html index d7b7cd8..a4b7c5b 100644 --- a/docs/v3/each.js.html +++ b/docs/v3/each.js.html @@ -106,37 +106,78 @@ import awaitify from './internal/awaitify' * @returns {Promise} a promise, if a callback is omitted * @example * - * // assuming openFiles is an array of file names and saveFile is a function - * // to save the modified contents of that file: + * // dir1 is a directory that contains file1.txt, file2.txt + * // dir2 is a directory that contains file3.txt, file4.txt + * // dir3 is a directory that contains file5.txt + * // dir4 does not exist * - * async.each(openFiles, saveFile, function(err){ - * // if any of the saves produced an error, err would equal that error - * }); - * - * // assuming openFiles is an array of file names - * async.each(openFiles, function(file, callback) { + * const fileList = [ 'dir1/file2.txt', 'dir2/file3.txt', 'dir/file5.txt']; + * const withMissingFileList = ['dir1/file1.txt', 'dir4/file2.txt']; * - * // Perform operation on file here. - * console.log('Processing file ' + file); + * // asynchronous function that deletes a file + * const deleteFile = function(file, callback) { + * fs.unlink(file, callback); + * }; * - * if( file.length > 32 ) { - * console.log('This file name is too long'); - * callback('File name too long'); - * } else { - * // Do work to process file here - * console.log('File processed'); - * callback(); - * } - * }, function(err) { - * // if any of the file processing produced an error, err would equal that error + * // Using callbacks + * async.each(fileList, deleteFile, function(err) { * if( err ) { - * // One of the iterations produced an error. - * // All processing will now stop. - * console.log('A file failed to process'); + * console.log(err); * } else { - * console.log('All files have been processed successfully'); + * console.log('All files have been deleted successfully'); * } * }); + * + * // Error Handling + * async.each(withMissingFileList, deleteFile, function(err){ + * console.log(err); + * // [ Error: ENOENT: no such file or directory ] + * // since dir4/file2.txt does not exist + * // dir1/file1.txt could have been deleted + * }); + * + * // Using Promises + * async.each(fileList, deleteFile) + * .then( () => { + * console.log('All files have been deleted successfully'); + * }).catch( err => { + * console.log(err); + * }); + * + * // Error Handling + * async.each(fileList, deleteFile) + * .then( () => { + * console.log('All files have been deleted successfully'); + * }).catch( err => { + * console.log(err); + * // [ Error: ENOENT: no such file or directory ] + * // since dir4/file2.txt does not exist + * // dir1/file1.txt could have been deleted + * }); + * + * // Using async/await + * async () => { + * try { + * await async.each(files, deleteFile); + * } + * catch (err) { + * console.log(err); + * } + * } + * + * // Error Handling + * async () => { + * try { + * await async.each(withMissingFileList, deleteFile); + * } + * catch (err) { + * console.log(err); + * // [ Error: ENOENT: no such file or directory ] + * // since dir4/file2.txt does not exist + * // dir1/file1.txt could have been deleted + * } + * } + * */ function eachLimit(coll, iteratee, callback) { return eachOf(coll, withoutIndex(wrapAsync(iteratee)), callback); |