A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/sindresorhus/p-filter below:

GitHub - sindresorhus/p-filter: Filter promises concurrently

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.

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

Type: Iterable<Promise<unknown> | unknown>

Iterated over concurrently in the filterer function.

Type: Function

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

Type: object

See the p-map options.

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

Type: Iterable<Promise<unknown> | unknown>

Iterated over concurrently in the filterer function.

Type: Function

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

Type: object

See the p-map options.

Type: number
Default: Infinity
Minimum: 1

The number of concurrently pending promises returned by filterer.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4