Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/p-filter
Filter promises concurrently
https://github.com/sindresorhus/p-filter
Last synced: about 1 month ago
JSON representation
Filter promises concurrently
- Host: GitHub
- URL: https://github.com/sindresorhus/p-filter
- Owner: sindresorhus
- License: mit
- Created: 2016-10-21T04:09:33.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2023-12-27T16:02:58.000Z (11 months ago)
- Last Synced: 2024-04-14T10:49:58.861Z (7 months ago)
- Language: JavaScript
- Size: 19.5 KB
- Stars: 70
- Watchers: 7
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- promise-fun - p-filter
README
# 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
```sh
npm install p-filter
```## Usage
```js
import pFilter from 'p-filter';
import getWeather from 'get-weather'; // Not a real moduleconst 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 | 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`.
#### options
Type: `object`
See the [`p-map` options](https://github.com/sindresorhus/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.
```js
import {pFilterIterable} from 'p-filter';
import getWeather from 'get-weather'; // Not a real moduleasync 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 | 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`.
#### options
Type: `object`
See the [`p-map` options](https://github.com/sindresorhus/p-map#options).
##### concurrency
Type: `number`\
Default: `Infinity`\
Minimum: `1`The number of concurrently pending promises returned by `filterer`.
## Related
- [p-locate](https://github.com/sindresorhus/p-locate) - Get the first fulfilled promise that satisfies the provided testing function
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
- [More…](https://github.com/sindresorhus/promise-fun)