Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/async/find.js
1126 views
1
'use strict';
2
3
Object.defineProperty(exports, "__esModule", {
4
value: true
5
});
6
7
var _createTester = require('./internal/createTester.js');
8
9
var _createTester2 = _interopRequireDefault(_createTester);
10
11
var _eachOf = require('./eachOf.js');
12
13
var _eachOf2 = _interopRequireDefault(_eachOf);
14
15
var _awaitify = require('./internal/awaitify.js');
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 {Promise} a promise, if a callback is omitted
47
* @example
48
*
49
* // dir1 is a directory that contains file1.txt, file2.txt
50
* // dir2 is a directory that contains file3.txt, file4.txt
51
* // dir3 is a directory that contains file5.txt
52
*
53
* // asynchronous function that checks if a file exists
54
* 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.txt
64
* // result now equals the first file in the list that exists
65
* }
66
*);
67
*
68
* // Using Promises
69
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists)
70
* .then(result => {
71
* console.log(result);
72
* // dir1/file1.txt
73
* // result now equals the first file in the list that exists
74
* }).catch(err => {
75
* console.log(err);
76
* });
77
*
78
* // Using async/await
79
* async () => {
80
* try {
81
* let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists);
82
* console.log(result);
83
* // dir1/file1.txt
84
* // result now equals the file in the list that exists
85
* }
86
* catch (err) {
87
* console.log(err);
88
* }
89
* }
90
*
91
*/
92
function detect(coll, iteratee, callback) {
93
return (0, _createTester2.default)(bool => bool, (res, item) => item)(_eachOf2.default, coll, iteratee, callback);
94
}
95
exports.default = (0, _awaitify2.default)(detect, 3);
96
module.exports = exports['default'];
97