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 /lib/groupBy.js | |
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 'lib/groupBy.js')
-rw-r--r-- | lib/groupBy.js | 68 |
1 files changed, 61 insertions, 7 deletions
diff --git a/lib/groupBy.js b/lib/groupBy.js index 2993367..c6949b2 100644 --- a/lib/groupBy.js +++ b/lib/groupBy.js @@ -27,15 +27,69 @@ import groupByLimit from './groupByLimit'; * @returns {Promise} a promise, if no callback is passed * @example * - * async.groupBy(['userId1', 'userId2', 'userId3'], function(userId, callback) { - * db.findById(userId, function(err, user) { - * if (err) return callback(err); - * return callback(null, user.age); + * // 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 + * + * const files = ['dir1/file1.txt','dir2','dir4'] + * + * // asynchronous function that detects file type as none, file, or directory + * function detectFile(file, callback) { + * fs.stat(file, function(err, stat) { + * if (err) { + * return callback(null, 'none'); + * } + * callback(null, stat.isDirectory() ? 'directory' : 'file'); * }); - * }, function(err, result) { - * // result is object containing the userIds grouped by age - * // e.g. { 30: ['userId1', 'userId3'], 42: ['userId2']}; + * } + * + * //Using callbacks + * async.groupBy(files, detectFile, function(err, result) { + * if(err) { + * console.log(err); + * } else { + * console.log(result); + * // { + * // file: [ 'dir1/file1.txt' ], + * // none: [ 'dir4' ], + * // directory: [ 'dir2'] + * // } + * // result is object containing the files grouped by type + * } * }); + * + * // Using Promises + * async.groupBy(files, detectFile) + * .then( result => { + * console.log(result); + * // { + * // file: [ 'dir1/file1.txt' ], + * // none: [ 'dir4' ], + * // directory: [ 'dir2'] + * // } + * // result is object containing the files grouped by type + * }).catch( err => { + * console.log(err); + * }); + * + * // Using async/await + * async () => { + * try { + * let result = await async.groupBy(files, detectFile); + * console.log(result); + * // { + * // file: [ 'dir1/file1.txt' ], + * // none: [ 'dir4' ], + * // directory: [ 'dir2'] + * // } + * // result is object containing the files grouped by type + * } + * catch (err) { + * console.log(err); + * } + * } + * */ export default function groupBy (coll, iteratee, callback) { return groupByLimit(coll, Infinity, iteratee, callback) |