You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently and get a filtered down result.
Install
npm install p-filter
Usage
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']
API
pFilter(input, filterer, options?)
Returns a Promise that is fulfilled when all promises in input and ones returned from filterer are fulfilled, or rejects if any of the promises reject. The fulfilled value is an Array of the fulfilled values returned from filterer in input order.
input
Type: Iterable<Promise<unknown> | unknown>
Iterated over concurrently in the filterer function.
filterer(element, index)
Type: Function
The filterer function that decides whether an element should be included into result. Expected to return boolean | Promise<boolean>.
The number of concurrently pending promises returned by filterer.
pFilterIterable(iterable, filterer, options?)
Returns an async iterable that iterates over the promises in iterable and ones returned from filterer concurrently, calling filterer 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']
iterable
Type: Iterable<Promise<unknown> | unknown>
Iterated over concurrently in the filterer function.
filterer(element, index)
Type: Function
The filterer function that decides whether an element should be included into result. Expected to return boolean | Promise<boolean>.