CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
detect.js96 linesDownload Raw Back to async
1'use strict';2 3Object.defineProperty(exports, "__esModule", {4    value: true5});6 7var _createTester = require('./internal/createTester.js');8 9var _createTester2 = _interopRequireDefault(_createTester);10 11var _eachOf = require('./eachOf.js');12 13var _eachOf2 = _interopRequireDefault(_eachOf);14 15var _awaitify = require('./internal/awaitify.js');16 17var _awaitify2 = _interopRequireDefault(_awaitify);18 19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }20 21/**22 * Returns the first value in `coll` that passes an async truth test. The23 * `iteratee` is applied in parallel, meaning the first iteratee to return24 * `true` will fire the detect `callback` with that result. That means the25 * result might not be the first item in the original `coll` (in terms of order)26 * that passes the test.27 28 * If order within the original `coll` is important, then look at29 * [`detectSeries`]{@link module:Collections.detectSeries}.30 *31 * @name detect32 * @static33 * @memberOf module:Collections34 * @method35 * @alias find36 * @category Collections37 * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.38 * @param {AsyncFunction} iteratee - A truth test to apply to each item in `coll`.39 * The iteratee must complete with a boolean value as its result.40 * Invoked with (item, callback).41 * @param {Function} [callback] - A callback which is called as soon as any42 * iteratee returns `true`, or after all the `iteratee` functions have finished.43 * Result will be the first item in the array that passes the truth test44 * (iteratee) or the value `undefined` if none passed. Invoked with45 * (err, result).46 * @returns {Promise} a promise, if a callback is omitted47 * @example48 *49 * // dir1 is a directory that contains file1.txt, file2.txt50 * // dir2 is a directory that contains file3.txt, file4.txt51 * // dir3 is a directory that contains file5.txt52 *53 * // asynchronous function that checks if a file exists54 * function fileExists(file, callback) {55 *    fs.access(file, fs.constants.F_OK, (err) => {56 *        callback(null, !err);57 *    });58 * }59 *60 * async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists,61 *    function(err, result) {62 *        console.log(result);63 *        // dir1/file1.txt64 *        // result now equals the first file in the list that exists65 *    }66 *);67 *68 * // Using Promises69 * async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists)70 * .then(result => {71 *     console.log(result);72 *     // dir1/file1.txt73 *     // result now equals the first file in the list that exists74 * }).catch(err => {75 *     console.log(err);76 * });77 *78 * // Using async/await79 * async () => {80 *     try {81 *         let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists);82 *         console.log(result);83 *         // dir1/file1.txt84 *         // result now equals the file in the list that exists85 *     }86 *     catch (err) {87 *         console.log(err);88 *     }89 * }90 *91 */92function detect(coll, iteratee, callback) {93    return (0, _createTester2.default)(bool => bool, (res, item) => item)(_eachOf2.default, coll, iteratee, callback);94}95exports.default = (0, _awaitify2.default)(detect, 3);96module.exports = exports.default;
basant307/AI_Governance_Project · CoolFace