Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/detectLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,50 @@ import awaitify from './internal/awaitify.js'
* (iteratee) or the value `undefined` if none passed. Invoked with
* (err, result).
* @returns {Promise} a promise, if a callback is omitted
* @example
*
* // dir1 is a directory that contains file1.txt, file2.txt
* // dir2 is a directory that contains file3.txt, file4.txt
* // dir3 is a directory that contains file5.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.detectLimit(['file3.txt','file2.txt','dir1/file1.txt'], 2, fileExists,
* function(err, result) {
* console.log(result);
* // dir1/file1.txt
* // result now equals the first file in the list that exists
* }
* );
*
* // Using Promises
* async.detectLimit(['file3.txt','file2.txt','dir1/file1.txt'], 2, fileExists)
* .then(result => {
* console.log(result);
* // dir1/file1.txt
* // result now equals the first file in the list that exists
* }).catch(err => {
* console.log(err);
* });
*
* // Using async/await
* async () => {
* try {
* let result = await async.detectLimit(['file3.txt','file2.txt','dir1/file1.txt'], 2, fileExists);
* console.log(result);
* // dir1/file1.txt
* // result now equals the first file in the list that exists
* }
* catch (err) {
* console.log(err);
* }
* }
*
*/
function detectLimit(coll, limit, iteratee, callback) {
return createTester(bool => bool, (res, item) => item)(eachOfLimit(limit), coll, iteratee, callback)
Expand Down