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

Filter promises concurrently

License

NotificationsYou must be signed in to change notification settings

sindresorhus/p-filter

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.

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

input

Type:Iterable<Promise<unknown> | unknown>

Iterated over concurrently in thefilterer function.

filterer(element, index)

Type:Function

The filterer function that decides whether an element should be included into result. Expected to returnboolean | Promise<boolean>.

options

Type:object

See thep-map options.

concurrency

Type:number
Default:Infinity
Minimum:1

The number of concurrently pending promises returned byfilterer.

pFilterIterable(iterable, filterer, options?)

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']

iterable

Type:Iterable<Promise<unknown> | unknown>

Iterated over concurrently in thefilterer function.

filterer(element, index)

Type:Function

The filterer function that decides whether an element should be included into result. Expected to returnboolean | Promise<boolean>.

options

Type:object

See thep-map options.

concurrency

Type:number
Default:Infinity
Minimum:1

The number of concurrently pending promises returned byfilterer.

Related

  • p-locate - Get the first fulfilled promise that satisfies the provided testing function
  • p-map - Map over promises concurrently
  • p-times - Run promise-returning & async functions a specific number of times concurrently
  • More…

About

Filter promises concurrently

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp