Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The p-any package is a utility for handling multiple promises in JavaScript. It returns a promise that is fulfilled when any of the input promises are fulfilled, or rejected if all of the input promises are rejected.
Basic Usage
This feature demonstrates the basic usage of p-any. It takes an array of promises and returns a promise that resolves as soon as the first promise in the array resolves.
const pAny = require('p-any');
const promise1 = Promise.resolve('First');
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'Second'));
const promise3 = new Promise((resolve, reject) => setTimeout(reject, 50, 'Third'));
pAny([promise1, promise2, promise3]).then(value => {
console.log(value); // 'First'
});
Handling Rejections
This feature demonstrates how p-any handles rejections. If all input promises are rejected, p-any returns an AggregateError.
const pAny = require('p-any');
const promise1 = new Promise((resolve, reject) => setTimeout(reject, 100, 'First'));
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 200, 'Second'));
const promise3 = new Promise((resolve, reject) => setTimeout(reject, 300, 'Third'));
pAny([promise1, promise2, promise3]).catch(error => {
console.log(error); // AggregateError: All promises were rejected
});
The promise-any package provides similar functionality to p-any. It also returns a promise that resolves as soon as any of the input promises resolve, or rejects if all input promises are rejected. The main difference is that promise-any is a polyfill for the Promise.any method introduced in ES2021.
Bluebird is a fully-featured promise library that includes a method called Bluebird.any, which provides similar functionality to p-any. Bluebird offers additional features and optimizations for working with promises, making it a more comprehensive solution for promise management.
Wait for any promise to be fulfilled
Useful when you need the fastest promise.
You probably want this instead of Promise.race()
. Reason.
With Node.js 15, there's now a built-in Promise#any
method. The benefit of this package is that it has cancellation functionality.
$ npm install p-any
Checks 3 websites and logs the fastest.
import pAny from 'p-any';
import got from 'got';
const first = await pAny([
got.head('https://github.com').then(() => 'github'),
got.head('https://google.com').then(() => 'google'),
got.head('https://twitter.com').then(() => 'twitter'),
]);
console.log(first);
//=> 'google'
Returns a cancelable Promise
that is fulfilled when any promise from input
is fulfilled. If all the input
promises reject, it will reject with an AggregateError
error.
Type: Iterable<Promise | unknown>
Type: object
Type: Function
Receives the value resolved by the promise. Used to filter out values that doesn't satisfy a condition.
Exposed for instance checking.
FAQs
Wait for any promise to be fulfilled
We found that p-any demonstrated a not healthy version release cadence and project activity because the last version was released 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.