diff --git a/lib/everyLimit.js b/lib/everyLimit.js index 6bb350aac..412d5f168 100644 --- a/lib/everyLimit.js +++ b/lib/everyLimit.js @@ -22,6 +22,33 @@ import awaitify from './internal/awaitify.js' * `iteratee` functions have finished. Result will be either `true` or `false` * depending on the values of the async tests. Invoked with (err, result). * @returns {Promise} a promise, if no callback provided + * @example + * + * // Check up to two files at a time. Stops early on the first `false`. + * // dir1 is a directory that contains file1.txt, file2.txt + * // dir2 is a directory that contains file3.txt, file4.txt + * const fileList = ['dir1/file1.txt','dir2/file3.txt','dir1/file2.txt']; + * + * // asynchronous function that checks if a file exists + * function fileExists(file, callback) { + * fs.access(file, fs.constants.F_OK, (err) => { + * callback(null, !err); + * }); + * } + * + * async.everyLimit(fileList, 2, fileExists, (err, result) => { + * if (err) throw err; + * console.log(result); + * // true + * // result is true since every file exists + * }); + * + * // Omit the callback and everyLimit returns a Promise. + * const allExist = await async.everyLimit(fileList, 2, async (file) => { + * return fs.promises.access(file).then(() => true, () => false); + * }); + * console.log(allExist); + * // true */ function everyLimit(coll, limit, iteratee, callback) { return createTester(bool => !bool, res => !res)(eachOfLimit(limit), coll, iteratee, callback)