Git Product home page Git Product logo

p-any's Introduction

p-any Build Status

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.

Install

$ npm install p-any

Usage

Checks 3 websites and logs the fastest.

const got = require('got');
const pAny = require('p-any');

(async () => {
	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'
})();

API

pAny(input, options?)

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.

input

Type: Iterable<Promise|any>

options

Type: object

filter

Type: Function

Receives the value resolved by the promise. Used to filter out values that doesn't satisfy a condition.

pAny.AggregateError

Exposed for instance checking.

Related

  • p-some - Wait for a specified number of promises to be fulfilled
  • p-locate - Get the first fulfilled promise that satisfies the provided testing function
  • More…

p-any's People

Contributors

sindresorhus avatar bendingbender avatar kevva avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.