
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
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 584,034 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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.