Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
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.
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);
Part of the 'async' utility module, this function offers asynchronous filtering capabilities. It is part of a larger suite of asynchronous control flow and collection processing utilities, which might make it a heavier dependency than p-filter if only filtering is needed.
Bluebird is a comprehensive promise library that includes a method called 'filter', which can be used for asynchronous filtering. It is more feature-rich than p-filter but also larger in size, which might be an overkill for simple use cases.
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.
$ npm install p-filter
const pFilter = require('p-filter');
const getWeather = require('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;
};
(async () => {
const result = await pFilter(places, filterer);
console.log(result);
//=> ['Bangkok, Thailand']
})();
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|any>
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
Type: number
Default: Infinity
Minimum: 1
Number of concurrently pending promises returned by filterer
.
MIT © Sindre Sorhus
FAQs
Filter promises concurrently
The npm package p-filter receives a total of 3,310,801 weekly downloads. As such, p-filter popularity was classified as popular.
We found that p-filter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.