Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
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
The npm package p-any receives a total of 463,574 weekly downloads. As such, p-any popularity was classified as popular.
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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.