Skip to content

Commit

Permalink
Added async.someLimit
Browse files Browse the repository at this point in the history
When checking if a folder contains subfolders or not (fs.readdir -> fs.stat -> some(is_a_Folder)), getting crashes when listing Windows shares from Ubuntu VM. someLimit avoids the problem.

Test for someLimit

added anyLimit alias and updated doc.

removed anyLimit alias
  • Loading branch information
mtone authored and megawac committed Jul 2, 2015
1 parent 0ed159a commit 06813bf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
28 changes: 26 additions & 2 deletions README.md
Expand Up @@ -172,8 +172,11 @@ Some functions are also available in the following forms:
* [`reduce`](#reduce), [`reduceRight`](#reduceRight)
* [`detect`](#detect), `detectSeries`
* [`sortBy`](#sortBy)
* [`some`](#some), [`every`](#every)
* [`concat`](#concat), `concatSeries`
* [`some`](#some)
* [`someLimit`](#someLimit)
* [`every`](#every)
* [`concat`](#concat)
* [`concatSeries`](#concatSeries)
### Control Flow
Expand Down Expand Up @@ -581,6 +584,27 @@ async.some(['file1','file2','file3'], fs.exists, function(result){

---------------------------------------

<a name="someLimit" />
### someLimit(arr, limit iterator, callback)

__Alias:__ `anyLimit`

The same as [`some`](#some), only no more than `limit` `iterator`s will be simultaneously
running at any time.

__Arguments__

* `arr` - An array to iterate over.
* `limit` - The maximum number of `iterator`s to run at any time.
* `iterator(item, callback)` - A truth test to apply to each item in the array
in parallel. The iterator is passed a callback(truthValue) which must be
called with a boolean argument once it has completed.
* `callback(result)` - A callback which is called as soon as any iterator returns
`true`, or after all the iterator functions have finished. Result will be
either `true` or `false` depending on the values of the async tests.

---------------------------------------

<a name="every" />
### every(arr, iterator, [callback])

Expand Down
20 changes: 15 additions & 5 deletions lib/async.js
Expand Up @@ -456,24 +456,34 @@
async.detectSeries = doSeries(_detect);

function _createTester(eachfn, check, defaultValue) {
return function(arr, iterator, main_callback) {
eachfn(arr, function (x, _, callback) {
return function(arr, limit, iterator, main_callback) {
function done() {
main_callback(defaultValue);
}
function iteratee(x, _, callback) {
iterator(x, function (v) {
if (check(v)) {
main_callback(!defaultValue);
main_callback = noop;
}
callback();
});
}, function () {
main_callback(defaultValue);
});
}
if (arguments.length > 3) {
eachfn(arr, limit, iteratee, done);
} else {
main_callback = iterator;
iterator = limit;
eachfn(arr, iteratee, done);
}
};
}

async.any =
async.some = _createTester(async.eachOf, identity, false);

async.someLimit = _createTester(async.eachOfLimit, identity, false);

async.all =
async.every = _createTester(async.eachOf, notId, true);

Expand Down
18 changes: 18 additions & 0 deletions test/test-async.js
Expand Up @@ -2236,6 +2236,24 @@ exports['some early return'] = function(test){
}, 100);
};

exports['someLimit true'] = function(test){
async.someLimit([3,1,2], 2, function(x, callback){
setTimeout(function(){callback(x === 2);}, 0);
}, function(result){
test.equals(result, true);
test.done();
});
};

exports['someLimit false'] = function(test){
async.someLimit([3,1,2], 2, function(x, callback){
setTimeout(function(){callback(x === 10);}, 0);
}, function(result){
test.equals(result, false);
test.done();
});
};

exports['any alias'] = function(test){
test.equals(async.any, async.some);
test.done();
Expand Down

0 comments on commit 06813bf

Please sign in to comment.