4.1.0 • Published 12 months ago

p-filter v4.1.0

Weekly downloads
1,748,690
License
MIT
Repository
github
Last release
12 months ago

p-filter

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

import pFilter from 'p-filter';
import getWeather from 'get-weather'; // Not a real module

const places = [
	getCapital('Norway').then(info => info.name),
	'Bangkok, Thailand',
	'Berlin, Germany',
	'Tokyo, Japan',
];

const filterer = async place => {
	const weather = await getWeather(place);
	return weather.temperature > 30;
};

const result = await pFilter(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>.

options

Type: object

See the p-map options.

concurrency

Type: number\ Default: Infinity\ Minimum: 1

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';
import getWeather from 'get-weather'; // Not a real module

async function * getPlaces() {
	const name = await getCapital('Norway');

	yield name;
	yield 'Bangkok, Thailand';
	yield 'Berlin, Germany';
	yield 'Tokyo, Japan';
}

const places = getPlaces();

const filterer = async place => {
	const weather = await getWeather(place);
	return weather.temperature > 30;
};

for await (const element of pFilterIterable(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>.

options

Type: object

See the p-map options.

concurrency

Type: number\ Default: Infinity\ Minimum: 1

The number of concurrently pending promises returned by filterer.

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…
cpy@june21x/semantic-release-github-no-commentarchetype-librarykilli8n-react-native-fast-imagepg-api-server@mink-opn/build-tokens@saaspe/components@everything-registry/sub-chunk-2396@rabailriaz/hisaab-web-portallapture-ui-complaptureuis3-moduleresume_fiveresume34567rmotsui-monoreposportscale-wifi-connectsequelcomponentremove-all-except-outer-linkssemantic-release-github-app-authsemantic-release-github-electron-buildersemantic-release-denosemantic-release-ghsemantic-release-gitmoji-actionsupisupi-glitchsvelte-component-libgomantic_releaseweb-yii2miguelcostero-ng2-toastygogencygogency-test-2json-sort-cliip-designlerna-clean-changelogs-clikahnhotkey-mapperhyperdrive-helpersgh-shipitgit-branching-workflowmediacp-suitep-thunkpackage-storeparvan_componentsparvan_reactjs_componentsp-funp-promise-utilspromise-funoi-greasenext-initnoddity-generator-clireact-flow-highly-customizable-edgesreact-form-component-libraryreact-picky-with-clearnth-git-toolsnetlify-clinetlify-cli-forknpm-install-torefinejs-repospigot-ui-inventorysort-json-cliskysyncsindresorhus.jstmp-fetch-enginetwine-libtwine-library@antongolub/semantic-release-github@aschmidt8/netlify-cli@bifravst/semantic-release-github@blazinaj/app-kit@billogram/janitor@career0.1/my-first-career0.1@career0.1/my-first-career0.2@bronosorg/graph-indexer-agent@bronosorg/graph-indexer-common@brickdoc/cpy@cjsa/cpy@ciberus/find-steam-app@changesets/read@achingbrain/semantic-release-github@coderesque/janitorxo-browser-focused-forkvue-v3-yandex-metrikaunrarbg@dword-design/maintenance@hickorytechnology/semantic-release-github@garden-io/core@garden-io/grow-plugin-kubernetes@gr2m/semantic-release-github@gr2m/semantic-release-github-487@foxpage/foxpage-cli@innodata/vue-v3-ya-metrika@infinitebrahmanuniverse/nolb-p-@lego-js/tests@motss-changesets/read@netlify/build@netlify/framework-info@niksy/postcss-sprites@ehmicky/dev-tasks
4.1.0

12 months ago

4.0.0

1 year ago

3.0.0

3 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.0

8 years ago