Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Test whether a collection contains `n` elements which pass a test implemented by a predicate function.
License
stdlib-js/utils-async-some-by
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out onGitHub, and please considerfinancially supporting stdlib. We greatly appreciate your continued support!
Test whether a collection contains at least
nelements which pass a test implemented by a predicate function.
npm install @stdlib/utils-async-some-by
Alternatively,
- To load the package in a website via a
scripttag without installation and bundlers, use theES Module available on theesmbranch (seeREADME). - If you are using Deno, visit the
denobranch (seeREADME for usage intructions). - For use in Observable, or in browser/node environments, use theUniversal Module Definition (UMD) build available on the
umdbranch (seeREADME).
Thebranches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
varsomeByAsync=require('@stdlib/utils-async-some-by');
Tests whether acollection contains at leastn elements which pass a test implemented by apredicate function.
functionpredicate(value,next){setTimeout(onTimeout,value);functiononTimeout(){console.log(value);/* => 1000 2500 3000 */next(null,false);}}functiondone(error,bool){if(error){throwerror;}console.log(bool);// => false}vararr=[3000,2500,1000];someByAsync(arr,2,predicate,done);
The function immediately stops processingcollection elements and returnstrue for the test result upon receivingn truthy predicate result values.
functionpredicate(value,index,next){setTimeout(onTimeout,value);functiononTimeout(){if(index===1){returnnext(newError('beep'));}next(null,true);}}functiondone(error,bool){if(error){throwerror;}console.log(bool);// => true}vararr=[3000,2500,1000];someByAsync(arr,1,predicate,done);
The function accepts the followingoptions:
limit: the maximum number of pending invocations at any one time. Default:infinity.series:booleanindicating whether to sequentially invoke thepredicatefunction for eachcollectionelement. Iftrue, the function setsoptions.limit=1. Default:false.thisArg: the execution context forpredicate.
By default, all elements are processed concurrently, which means that the function doesnot guarantee completion order. To process eachcollection element sequentially, set theseries option totrue.
functionpredicate(value,next){setTimeout(onTimeout,value);functiononTimeout(){console.log(value);/* => 3000 2500 1000 */next(null,false);}}functiondone(error,bool){if(error){throwerror;}console.log(bool);// => false}vararr=[3000,2500,1000];varopts={'series':true};someByAsync(arr,2,opts,predicate,done);
To limit the maximum number of pending function invocations, set thelimit option.
functionpredicate(value,next){setTimeout(onTimeout,value);functiononTimeout(){console.log(value);/* => 2500 3000 1000 */next(null,false);}}functiondone(error,bool){if(error){throwerror;}console.log(bool);// => false}vararr=[3000,2500,1000];varopts={'limit':2};someByAsync(arr,2,opts,predicate,done);
To set the execution context of thepredicate function, set thethisArg option.
functionpredicate(value,next){this.count+=1;setTimeout(onTimeout,value);functiononTimeout(){next(null,false);}}vararr=[3000,2500,1000];varcontext={'count':0};varopts={'thisArg':context};someByAsync(arr,2,opts,predicate,done);functiondone(error,bool){if(error){throwerror;}console.log(bool);// => falseconsole.log(context.count);// => 3}
When invoked, thepredicate function is provided a maximum of four arguments:
value: collection value.index: collection index.collection: the inputcollection.next: a callback which should be called once thepredicatefunction has finished processing a collectionvalue.
The actual number of provided arguments depends on functionlength. If thepredicate function accepts two arguments, thepredicate function is providedvalue andnext. If thepredicate function accepts three arguments, thepredicate function is providedvalue,index, andnext. For every otherpredicate function signature, thepredicate function is provided all four arguments.
functionpredicate(value,i,collection,next){console.log('collection: %s. %d: %d',collection.join(','),i,value);/* => collection: 3000,2500,1000. 0: 3000 collection: 3000,2500,1000. 1: 2500 collection: 3000,2500,1000. 2: 1000 */setTimeout(onTimeout,value);functiononTimeout(){console.log(value);/* => 1000 2500 3000 */next(null,false);}}functiondone(error,bool){if(error){throwerror;}console.log(bool);// => false}vararr=[3000,2500,1000];someByAsync(arr,2,predicate,done);
Returns afunction which invokes apredicate function once for each element in acollection.
functionpredicate(value,next){setTimeout(onTimeout,value);functiononTimeout(){console.log(value);next(null,false);}}functiondone(error,bool){if(error){throwerror;}console.log(bool);}varf=someByAsync.factory(predicate);vararr1=[3000,2500,1000];f(arr1,2,done);/* e.g., => 1000 2500 3000 false*/vararr2=[300,250,100];f(arr2,2,done);/* e.g., => 100 250 300 false*/
The function accepts the sameoptions assomeByAsync().
- A
collectionmay be either anArray,Typed Array, or an array-likeObject(excludingstringsandfunctions). - If a provided function calls the
nextcallback with a truthyerrorargument, the function suspends execution and immediately calls thedonecallback for subsequenterrorhandling. - The function doesnot support dynamic
collectionresizing. - The function doesnot skip
undefinedelements. - If provided an empty
collection, the function calls thedonecallback withfalseas the test result. - Neither
someByAsyncnor the function returned by thefactorymethodguarantee asynchronous execution. To guarantee asynchrony, wrap thedonecallback in a function which either executes at the end of the current stack (e.g.,nextTick) or during a subsequent turn of the event loop (e.g.,setImmediate,setTimeout).
varresolve=require('path').resolve;varreadFile=require('@stdlib/fs-read-file');varsomeByAsync=require('@stdlib/utils-async-some-by');varfiles=[resolve(__dirname,'package.json'),resolve(__dirname,'README.md')];functiondone(error,bool){if(error){throwerror;}if(bool){console.log('Successfully read some files.');}else{console.log('Unable to read some files.');}}functionpredicate(file,next){varopts={'encoding':'utf8'};readFile(file,opts,onFile);functiononFile(error){if(error){returnnext(null,false);}next(null,true);}}someByAsync(files,2,predicate,done);
@stdlib/utils-async/any-by:test whether at least one element in a collection passes a test implemented by a predicate function.@stdlib/utils-async/every-by:test whether all elements in a collection pass a test implemented by a predicate function.@stdlib/utils-async/for-each:invoke a function once for each element in a collection.@stdlib/utils-async/none-by:test whether all elements in a collection fail a test implemented by a predicate function.@stdlib/utils-some-by:test whether a collection contains at leastnelements which pass a test implemented by a predicate function.@stdlib/utils-async/some-by-right:test whether a collection contains at leastnelements which pass a test implemented by a predicate function, iterating from right to left.
This package is part ofstdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to developstdlib, see the main projectrepository.
SeeLICENSE.
Copyright © 2016-2025. The StdlibAuthors.
About
Test whether a collection contains `n` elements which pass a test implemented by a predicate function.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.