diff options
author | Roman Lorenzo Balayan <roman.balayan@gmail.com> | 2021-08-06 06:58:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-05 15:58:10 -0700 |
commit | 159a119fbb1a585c61f33b4b3b5036f5d332ebbb (patch) | |
tree | bbf78b2e8899c412ceede4a705d45368f61b51bf /docs/v3/map.js.html | |
parent | 89255fe326050e80ce5394a9c00e11e9be8b1005 (diff) | |
download | async-159a119fbb1a585c61f33b4b3b5036f5d332ebbb.tar.gz |
Enhance examples for Collections methods. (#1726)
* Enhance examples for Collections methods.
* Update collection example code, avoiding usage of IIFE on async/await examples
* Convert examples on async.auto, async.series, and async.parallel to samples using Promises and async/await
Co-authored-by: Roman Balayan <roman.balayan@paymaya.com>
Diffstat (limited to 'docs/v3/map.js.html')
-rw-r--r-- | docs/v3/map.js.html | 84 |
1 files changed, 82 insertions, 2 deletions
diff --git a/docs/v3/map.js.html b/docs/v3/map.js.html index dc5dd00..b527c06 100644 --- a/docs/v3/map.js.html +++ b/docs/v3/map.js.html @@ -112,9 +112,89 @@ import awaitify from './internal/awaitify' * @returns {Promise} a promise, if no callback is passed * @example * - * async.map(['file1','file2','file3'], fs.stat, function(err, results) { - * // results is now an array of stats for each file + * // file1.txt is a file that is 1000 bytes in size + * // file2.txt is a file that is 2000 bytes in size + * // file3.txt is a file that is 3000 bytes in size + * // file4.txt does not exist + * + * const fileList = ['file1.txt','file2.txt','file3.txt']; + * const withMissingFileList = ['file1.txt','file2.txt','file4.txt']; + * + * // asynchronous function that returns the file size in bytes + * function getFileSizeInBytes(file, callback) { + * fs.stat(file, function(err, stat) { + * if (err) { + * return callback(err); + * } + * callback(null, stat.size); + * }); + * } + * + * // Using callbacks + * async.map(fileList, getFileSizeInBytes, function(err, results) { + * if (err) { + * console.log(err); + * } else { + * console.log(results); + * // results is now an array of the file size in bytes for each file, e.g. + * // [ 1000, 2000, 3000] + * } + * }); + * + * // Error Handling + * async.map(withMissingFileList, getFileSizeInBytes, function(err, results) { + * if (err) { + * console.log(err); + * // [ Error: ENOENT: no such file or directory ] + * } else { + * console.log(results); + * } + * }); + * + * // Using Promises + * async.map(fileList, getFileSizeInBytes) + * .then( results => { + * console.log(results); + * // results is now an array of the file size in bytes for each file, e.g. + * // [ 1000, 2000, 3000] + * }).catch( err => { + * console.log(err); + * }); + * + * // Error Handling + * async.map(withMissingFileList, getFileSizeInBytes) + * .then( results => { + * console.log(results); + * }).catch( err => { + * console.log(err); + * // [ Error: ENOENT: no such file or directory ] * }); + * + * // Using async/await + * async () => { + * try { + * let results = await async.map(fileList, getFileSizeInBytes); + * console.log(results); + * // results is now an array of the file size in bytes for each file, e.g. + * // [ 1000, 2000, 3000] + * } + * catch (err) { + * console.log(err); + * } + * } + * + * // Error Handling + * async () => { + * try { + * let results = await async.map(withMissingFileList, getFileSizeInBytes); + * console.log(results); + * } + * catch (err) { + * console.log(err); + * // [ Error: ENOENT: no such file or directory ] + * } + * } + * */ function map (coll, iteratee, callback) { return _map(eachOf, coll, iteratee, callback) |