Skip to content
Wait for a specified number of promises to be fulfilled
JavaScript TypeScript
Branch: master
Clone or download
Latest commit 57614d1 Apr 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig Meta tweaks Jun 12, 2017
.gitattributes
.gitignore
.npmrc Require Node.js 6 May 26, 2018
.travis.yml Require Node.js 8, add TypeScript definition (#9) Mar 8, 2019
index.d.ts Refactor TypeScript definition to CommonJS compatible export (#11) Apr 5, 2019
index.js
index.test-d.ts Refactor TypeScript definition to CommonJS compatible export (#11) Apr 5, 2019
license Meta tweaks Jun 12, 2017
package.json 4.1.0 Apr 5, 2019
readme.md
test.js Require Node.js 8, add TypeScript definition (#9) Mar 8, 2019

readme.md

p-some Build Status

Wait for a specified number of promises to be fulfilled

Useful when you need the fastest of multiple promises.

Install

$ npm install p-some

Usage

Checks 4 websites and logs the 2 fastest.

const got = require('got');
const pSome = require('p-some');

(async () => {
	const input = [
		got.head('github.com').then(() => 'github'),
		got.head('google.com').then(() => 'google'),
		got.head('twitter.com').then(() => 'twitter'),
		got.head('medium.com').then(() => 'medium')
	];

	const [first, second] = await pSome(input, {count: 2});

	console.log(first, second);
	//=> 'google twitter'
})();

API

pSome(input, options)

Returns a cancelable Promise that is fulfilled when count promises from input are fulfilled. The fulfilled value is an Array of the values from the input promises in the order they were fulfilled. If it becomes impossible to satisfy count, for example, too many promises rejected, it will reject with an AggregateError error.

If you pass in cancelable promises, specifically promises with a .cancel() method, that method will be called for the promises that are still unfulfilled when the returned Promise is either fulfilled or rejected.

input

Type: Iterable<Promise | unknown>

An Iterable collection of promises/values to wait for.

options

Type: Object

count

Required
Type: number
Minimum: 1

Number of promises from input that have to be fulfilled until the returned promise is fulfilled.

filter

Type: Function

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

pSome.AggregateError

Exposed for instance checking.

Related

License

MIT © Sindre Sorhus

You can’t perform that action at this time.