You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

p-filter

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-filter

Filter promises concurrently


Version published
Maintainers
1
Created

Package description

What is p-filter?

The p-filter npm package is a utility that allows you to filter collections of items using asynchronous predicate functions. It is particularly useful when you need to perform some asynchronous operation to determine whether an item should be included in the resulting array.

What are p-filter's main functionalities?

Asynchronous Filtering

This feature allows you to filter an array asynchronously. The `getIsAnimal` function might perform an asynchronous operation, such as fetching data from an API to determine if the name belongs to an animal. The `pFilter` function will then return a new array containing only the names that are animals.

const pFilter = require('p-filter');
const getIsAnimal = async (name) => { /* ... */ };
const names = ['Sphinx', 'Hydra', 'Human'];
const animals = await pFilter(names, getIsAnimal);

Other packages similar to p-filter

Readme

Source

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.

  • 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…

Keywords

FAQs

Package last updated on 27 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc