detect.js
1 'use strict'; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 7 var _createTester = require('./internal/createTester'); 8 9 var _createTester2 = _interopRequireDefault(_createTester); 10 11 var _eachOf = require('./eachOf'); 12 13 var _eachOf2 = _interopRequireDefault(_eachOf); 14 15 var _awaitify = require('./internal/awaitify'); 16 17 var _awaitify2 = _interopRequireDefault(_awaitify); 18 19 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 20 21 /** 22 * Returns the first value in `coll` that passes an async truth test. The 23 * `iteratee` is applied in parallel, meaning the first iteratee to return 24 * `true` will fire the detect `callback` with that result. That means the 25 * 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 at 29 * [`detectSeries`]{@link module:Collections.detectSeries}. 30 * 31 * @name detect 32 * @static 33 * @memberOf module:Collections 34 * @method 35 * @alias find 36 * @category Collections 37 * @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 any 42 * 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 test 44 * (iteratee) or the value `undefined` if none passed. Invoked with 45 * (err, result). 46 * @returns A Promise, if no callback is passed 47 * @example 48 * 49 * async.detect(['file1','file2','file3'], function(filePath, callback) { 50 * fs.access(filePath, function(err) { 51 * callback(null, !err) 52 * }); 53 * }, function(err, result) { 54 * // result now equals the first file in the list that exists 55 * }); 56 */ 57 function detect(coll, iteratee, callback) { 58 return (0, _createTester2.default)(bool => bool, (res, item) => item)(_eachOf2.default, coll, iteratee, callback); 59 } 60 exports.default = (0, _awaitify2.default)(detect, 3); 61 module.exports = exports['default'];