- Notifications
You must be signed in to change notification settings - Fork8
Filter promises concurrently
License
sindresorhus/p-filter
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Filter promises concurrently
Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently and get a filtered down result.
npm install p-filter
importpFilterfrom'p-filter';importgetWeatherfrom'get-weather';// Not a real moduleconstplaces=[getCapital('Norway').then(info=>info.name),'Bangkok, Thailand','Berlin, Germany','Tokyo, Japan',];constfilterer=asyncplace=>{constweather=awaitgetWeather(place);returnweather.temperature>30;};constresult=awaitpFilter(places,filterer);console.log(result);//=> ['Bangkok, Thailand']
Returns aPromise
that is fulfilled when all promises ininput
and ones returned fromfilterer
are fulfilled, or rejects if any of the promises reject. The fulfilled value is anArray
of the fulfilled values returned fromfilterer
ininput
order.
Type:Iterable<Promise<unknown> | unknown>
Iterated over concurrently in thefilterer
function.
Type:Function
The filterer function that decides whether an element should be included into result. Expected to returnboolean | Promise<boolean>
.
Type:object
See thep-map
options.
Type:number
Default:Infinity
Minimum:1
The number of concurrently pending promises returned byfilterer
.
Returns an async iterable that iterates over the promises initerable
and ones returned fromfilterer
concurrently, callingfilterer
for each element.
import{pFilterIterable}from'p-filter';importgetWeatherfrom'get-weather';// Not a real moduleasyncfunction*getPlaces(){constname=awaitgetCapital('Norway');yieldname;yield'Bangkok, Thailand';yield'Berlin, Germany';yield'Tokyo, Japan';}constplaces=getPlaces();constfilterer=asyncplace=>{constweather=awaitgetWeather(place);returnweather.temperature>30;};forawait(constelementofpFilterIterable(places,filterer)){console.log(element);}//=> ['Bangkok, Thailand']
Type:Iterable<Promise<unknown> | unknown>
Iterated over concurrently in thefilterer
function.
Type:Function
The filterer function that decides whether an element should be included into result. Expected to returnboolean | Promise<boolean>
.
Type:object
See thep-map
options.
Type:number
Default:Infinity
Minimum:1
The number of concurrently pending promises returned byfilterer
.
About
Filter promises concurrently