Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Test whether a collection contains `n` elements which pass a test implemented by a predicate function.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/utils-async-some-by

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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!

someByAsync

NPM versionBuild StatusCoverage Status

Test whether a collection contains at leastn elements which pass a test implemented by a predicate function.

Usage

importsomeByAsyncfrom'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-some-by@deno/mod.js';

You can also import the following named exports from the package:

import{factory}from'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-some-by@deno/mod.js';

someByAsync( collection, n, [options,] predicate, done )

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:boolean indicating whether to sequentially invoke thepredicate function for eachcollection element. 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 thepredicate function 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);

someByAsync.factory( [options,] predicate )

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().

Notes

  • Acollection may be either anArray,Typed Array, or an array-likeObject (excludingstrings andfunctions).
  • If a provided function calls thenext callback with a truthyerror argument, the function suspends execution and immediately calls thedone callback for subsequenterror handling.
  • The function doesnot support dynamiccollection resizing.
  • The function doesnot skipundefined elements.
  • If provided an emptycollection, the function calls thedone callback withfalse as the test result.
  • NeithersomeByAsync nor the function returned by thefactory methodguarantee asynchronous execution. To guarantee asynchrony, wrap thedone 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).

Examples

varresolve=require('path').resolve;importreadFilefrom'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@deno/mod.js';importsomeByAsyncfrom'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-some-by@deno/mod.js';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);

See Also


Notice

This package is part ofstdlib, a standard library 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.

Community

Chat


License

SeeLICENSE.

Copyright

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

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp