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/detect.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/detect.js')
-rw-r--r-- | lib/detect.js | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/lib/detect.js b/lib/detect.js index 4a6746a..0b63df4 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -30,13 +30,48 @@ import awaitify from './internal/awaitify' * @returns A Promise, if no callback is passed * @example * - * async.detect(['file1','file2','file3'], function(filePath, callback) { - * fs.access(filePath, function(err) { - * callback(null, !err) - * }); - * }, function(err, result) { + * // 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 + * + * // asynchronous function that checks if a file exists + * function fileExists(file, callback) { + * fs.access(file, fs.constants.F_OK, (err) => { + * callback(null, !err); + * }); + * } + * + * async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists, + * function(err, result) { + * console.log(result); + * // dir1/file1.txt + * // result now equals the first file in the list that exists + * } + *); + * + * // Using Promises + * async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists) + * .then(result => { + * console.log(result); + * // dir1/file1.txt * // result now equals the first file in the list that exists + * }).catch(err => { + * console.log(err); * }); + * + * // Using async/await + * async () => { + * try { + * let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists); + * console.log(result); + * // dir1/file1.txt + * // result now equals the file in the list that exists + * } + * catch (err) { + * console.log(err); + * } + * } + * */ function detect(coll, iteratee, callback) { return createTester(bool => bool, (res, item) => item)(eachOf, coll, iteratee, callback) |