Gathering detailed insights and metrics for @stdlib/utils-async-any-by
Gathering detailed insights and metrics for @stdlib/utils-async-any-by
Gathering detailed insights and metrics for @stdlib/utils-async-any-by
Gathering detailed insights and metrics for @stdlib/utils-async-any-by
@stdlib/utils-async-any-by-right
Test whether at least one element in a collection passes a test implemented by a predicate function, iterating from right to left.
@stdlib/utils-async-some-by
Test whether a collection contains `n` elements which pass a test implemented by a predicate function.
@stdlib/utils-async-some-by-right
Test whether a collection contains at least `n` elements which pass a test implemented by a predicate function, iterating from right to left.
Test whether at least one element in a collection passes a test implemented by a predicate function.
npm install @stdlib/utils-async-any-by
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
2 Stars
70 Commits
3 Watchers
5 Branches
14 Contributors
Updated on Feb 24, 2025
Latest Version
0.2.2
Package Id
@stdlib/utils-async-any-by@0.2.2
Unpacked Size
78.79 kB
Size
15.41 kB
File Count
14
NPM Version
8.19.4
Node Version
16.20.2
Published on
Jul 28, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
11
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 on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Test whether at least one element in a collection passes a test implemented by a predicate function.
1npm install @stdlib/utils-async-any-by
1var anyByAsync = require( '@stdlib/utils-async-any-by' );
Tests whether at least one element in a collection
passes a test implemented by a predicate
function.
1function predicate( value, next ) { 2 setTimeout( onTimeout, value ); 3 function onTimeout() { 4 console.log( value ); 5 /* => 6 1000 7 2500 8 3000 9 */ 10 11 next( null, false ); 12 } 13} 14 15function done( error, bool ) { 16 if ( error ) { 17 throw error; 18 } 19 console.log( bool ); 20 // => false 21} 22 23var arr = [ 3000, 2500, 1000 ]; 24 25anyByAsync( arr, predicate, done );
If a predicate
function calls the next
callback with a truthy test argument, the function stops processing any additional collection
elements and returns true
for the test result.
1function predicate( value, index, next ) { 2 setTimeout( onTimeout, value ); 3 function onTimeout() { 4 if ( index === 1 ) { 5 return next( null, true ); 6 } 7 next( null, false ); 8 } 9} 10 11function done( error, bool ) { 12 if ( error ) { 13 throw error; 14 } 15 console.log( bool ); 16 // => true 17} 18 19var arr = [ 3000, 2500, 1000 ]; 20 21anyByAsync( arr, predicate, done );
The function accepts the following options
:
limit
: the maximum number of pending invocations at any one time. Default: infinity
.series
: boolean
indicating whether to sequentially invoke the predicate
function for each collection
element. If true
, the function sets options.limit=1
. Default: false
.thisArg
: the execution context for predicate
.By default, all elements are processed concurrently, which means that the function does not guarantee completion order. To process each collection
element sequentially, set the series
option to true
.
1function predicate( value, next ) { 2 setTimeout( onTimeout, value ); 3 function onTimeout() { 4 console.log( value ); 5 /* => 6 3000 7 2500 8 1000 9 */ 10 11 next( null, false ); 12 } 13} 14 15function done( error, bool ) { 16 if ( error ) { 17 throw error; 18 } 19 console.log( bool ); 20 // => false 21} 22 23var arr = [ 3000, 2500, 1000 ]; 24 25var opts = { 26 'series': true 27}; 28 29anyByAsync( arr, opts, predicate, done );
To limit the maximum number of pending function invocations, set the limit
option.
1function predicate( value, next ) { 2 setTimeout( onTimeout, value ); 3 function onTimeout() { 4 console.log( value ); 5 /* => 6 2500 7 3000 8 1000 9 */ 10 11 next( null, false ); 12 } 13} 14 15function done( error, bool ) { 16 if ( error ) { 17 throw error; 18 } 19 console.log( bool ); 20 // => false 21} 22 23var arr = [ 3000, 2500, 1000 ]; 24 25var opts = { 26 'limit': 2 27}; 28 29anyByAsync( arr, opts, predicate, done );
To set the execution context of the predicate
function, set the thisArg
option.
1function predicate( value, next ) { 2 this.count += 1; 3 setTimeout( onTimeout, value ); 4 function onTimeout() { 5 next( null, false ); 6 } 7} 8 9var arr = [ 3000, 2500, 1000 ]; 10 11var context = { 12 'count': 0 13}; 14 15var opts = { 16 'thisArg': context 17}; 18 19anyByAsync( arr, opts, predicate, done ); 20 21function done( error, bool ) { 22 if ( error ) { 23 throw error; 24 } 25 console.log( bool ); 26 // => false 27 28 console.log( context.count ); 29 // => 3 30}
When invoked, the predicate
function is provided a maximum of four arguments:
value
: collection value.index
: collection index.collection
: the input collection
.next
: a callback which should be called once the predicate
function has finished processing a collection value
.The actual number of provided arguments depends on function length
. If the predicate
function accepts two arguments, the predicate
function is provided value
and next
. If the predicate
function accepts three arguments, the predicate
function is provided value
, index
, and next
. For every other predicate
function signature, the predicate
function is provided all four arguments.
1function predicate( value, i, collection, next ) { 2 console.log( 'collection: %s. %d: %d', collection.join( ',' ), i, value ); 3 /* => 4 collection: 3000,2500,1000. 0: 3000 5 collection: 3000,2500,1000. 1: 2500 6 collection: 3000,2500,1000. 2: 1000 7 */ 8 setTimeout( onTimeout, value ); 9 function onTimeout() { 10 console.log( value ); 11 /* => 12 1000 13 2500 14 3000 15 */ 16 17 next( null, false ); 18 } 19} 20 21function done( error, bool ) { 22 if ( error ) { 23 throw error; 24 } 25 console.log( bool ); 26 // => false 27} 28 29var arr = [ 3000, 2500, 1000 ]; 30 31anyByAsync( arr, predicate, done );
Returns a function
which invokes a predicate
function once for each element in a collection
.
1function predicate( value, next ) { 2 setTimeout( onTimeout, value ); 3 function onTimeout() { 4 console.log( value ); 5 next( null, false ); 6 } 7} 8 9function done( error, bool ) { 10 if ( error ) { 11 throw error; 12 } 13 console.log( bool ); 14} 15 16var f = anyByAsync.factory( predicate ); 17 18var arr1 = [ 3000, 2500, 1000 ]; 19 20f( arr1, done ); 21/* e.g., => 22 1000 23 2500 24 3000 25 false 26*/ 27 28var arr2 = [ 300, 250, 100 ]; 29 30f( arr2, done ); 31/* e.g., => 32 100 33 250 34 300 35 false 36*/
The function accepts the same options
as anyByAsync()
.
collection
may be either an Array
, Typed Array
, or an array-like Object
(excluding strings
and functions
).next
callback with a truthy error
argument, the function suspends execution and immediately calls the done
callback for subsequent error
handling.collection
resizing.undefined
elements.collection
, the function calls the done
callback with false
as the test result.anyByAsync
nor the function returned by the factory
method guarantee asynchronous execution. To guarantee asynchrony, wrap the done
callback 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
).1var resolve = require( 'path' ).resolve; 2var readFile = require( '@stdlib/fs-read-file' ); 3var anyByAsync = require( '@stdlib/utils-async-any-by' ); 4 5var files = [ 6 resolve( __dirname, 'package.json' ), 7 resolve( __dirname, 'README.md' ) 8]; 9 10function done( error, bool ) { 11 if ( error ) { 12 throw error; 13 } 14 if ( bool ) { 15 console.log( 'Successfully read at least one file.' ); 16 } else { 17 console.log( 'Unable to read any files.' ); 18 } 19} 20 21function predicate( file, next ) { 22 var opts = { 23 'encoding': 'utf8' 24 }; 25 readFile( file, opts, onFile ); 26 27 function onFile( error ) { 28 if ( error ) { 29 return next( null, false ); 30 } 31 next( null, true ); 32 } 33} 34 35anyByAsync( files, predicate, done );
@stdlib/utils-any-by
: test whether at least one element in a collection passes a test implemented by a predicate function.@stdlib/utils-async/any-by-right
: test whether at least one element in a collection passes a test implemented by a predicate function, iterating from right to left.@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-async/some-by
: test whether a collection contains n
elements which pass a test implemented by a predicate function.This package is part of stdlib, 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 develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
no SAST tool detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More