every.js 3.5 KB

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