some.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createTester = require('./internal/createTester.js');
  6. var _createTester2 = _interopRequireDefault(_createTester);
  7. var _eachOf = require('./eachOf.js');
  8. var _eachOf2 = _interopRequireDefault(_eachOf);
  9. var _awaitify = require('./internal/awaitify.js');
  10. var _awaitify2 = _interopRequireDefault(_awaitify);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Returns `true` if at least one element in the `coll` satisfies an async test.
  14. * If any iteratee call returns `true`, the main `callback` is immediately
  15. * called.
  16. *
  17. * @name some
  18. * @static
  19. * @memberOf module:Collections
  20. * @method
  21. * @alias any
  22. * @category Collection
  23. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  24. * @param {AsyncFunction} iteratee - An async truth test to apply to each item
  25. * in the collections in parallel.
  26. * The iteratee should complete with a boolean `result` value.
  27. * Invoked with (item, callback).
  28. * @param {Function} [callback] - A callback which is called as soon as any
  29. * iteratee returns `true`, or after all the iteratee functions have finished.
  30. * Result will be either `true` or `false` depending on the values of the async
  31. * tests. Invoked with (err, result).
  32. * @returns {Promise} a promise, if no callback provided
  33. * @example
  34. *
  35. * // dir1 is a directory that contains file1.txt, file2.txt
  36. * // dir2 is a directory that contains file3.txt, file4.txt
  37. * // dir3 is a directory that contains file5.txt
  38. * // dir4 does not exist
  39. *
  40. * // asynchronous function that checks if a file exists
  41. * function fileExists(file, callback) {
  42. * fs.access(file, fs.constants.F_OK, (err) => {
  43. * callback(null, !err);
  44. * });
  45. * }
  46. *
  47. * // Using callbacks
  48. * async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists,
  49. * function(err, result) {
  50. * console.log(result);
  51. * // true
  52. * // result is true since some file in the list exists
  53. * }
  54. *);
  55. *
  56. * async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists,
  57. * function(err, result) {
  58. * console.log(result);
  59. * // false
  60. * // result is false since none of the files exists
  61. * }
  62. *);
  63. *
  64. * // Using Promises
  65. * async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists)
  66. * .then( result => {
  67. * console.log(result);
  68. * // true
  69. * // result is true since some file in the list exists
  70. * }).catch( err => {
  71. * console.log(err);
  72. * });
  73. *
  74. * async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists)
  75. * .then( result => {
  76. * console.log(result);
  77. * // false
  78. * // result is false since none of the files exists
  79. * }).catch( err => {
  80. * console.log(err);
  81. * });
  82. *
  83. * // Using async/await
  84. * async () => {
  85. * try {
  86. * let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists);
  87. * console.log(result);
  88. * // true
  89. * // result is true since some file in the list exists
  90. * }
  91. * catch (err) {
  92. * console.log(err);
  93. * }
  94. * }
  95. *
  96. * async () => {
  97. * try {
  98. * let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists);
  99. * console.log(result);
  100. * // false
  101. * // result is false since none of the files exists
  102. * }
  103. * catch (err) {
  104. * console.log(err);
  105. * }
  106. * }
  107. *
  108. */
  109. function some(coll, iteratee, callback) {
  110. return (0, _createTester2.default)(Boolean, res => res)(_eachOf2.default, coll, iteratee, callback);
  111. }
  112. exports.default = (0, _awaitify2.default)(some, 3);
  113. module.exports = exports['default'];