summaryrefslogtreecommitdiff
path: root/lib/detect.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/detect.js')
-rw-r--r--lib/detect.js45
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)