summaryrefslogtreecommitdiff
path: root/docs/v3/map.js.html
diff options
context:
space:
mode:
authorRoman Lorenzo Balayan <roman.balayan@gmail.com>2021-08-06 06:58:10 +0800
committerGitHub <noreply@github.com>2021-08-05 15:58:10 -0700
commit159a119fbb1a585c61f33b4b3b5036f5d332ebbb (patch)
treebbf78b2e8899c412ceede4a705d45368f61b51bf /docs/v3/map.js.html
parent89255fe326050e80ce5394a9c00e11e9be8b1005 (diff)
downloadasync-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.html84
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 &apos;./internal/awaitify&apos;
* @returns {Promise} a promise, if no callback is passed
* @example
*
- * async.map([&apos;file1&apos;,&apos;file2&apos;,&apos;file3&apos;], 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 = [&apos;file1.txt&apos;,&apos;file2.txt&apos;,&apos;file3.txt&apos;];
+ * const withMissingFileList = [&apos;file1.txt&apos;,&apos;file2.txt&apos;,&apos;file4.txt&apos;];
+ *
+ * // 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 =&gt; {
+ * console.log(results);
+ * // results is now an array of the file size in bytes for each file, e.g.
+ * // [ 1000, 2000, 3000]
+ * }).catch( err =&gt; {
+ * console.log(err);
+ * });
+ *
+ * // Error Handling
+ * async.map(withMissingFileList, getFileSizeInBytes)
+ * .then( results =&gt; {
+ * console.log(results);
+ * }).catch( err =&gt; {
+ * console.log(err);
+ * // [ Error: ENOENT: no such file or directory ]
* });
+ *
+ * // Using async/await
+ * async () =&gt; {
+ * 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 () =&gt; {
+ * 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)