Comparing version 2.0.1 to 3.0.0
53
index.js
'use strict'; | ||
const AggregateError = require('aggregate-error'); | ||
const PCancelable = require('p-cancelable'); | ||
module.exports = (iterable, opts) => new Promise((resolve, reject) => { | ||
opts = Object.assign({}, opts); | ||
module.exports = (iterable, options) => new PCancelable((resolve, reject, onCancel) => { | ||
options = Object.assign({}, options); | ||
if (!Number.isFinite(opts.count)) { | ||
throw new TypeError(`Expected a finite number, got ${typeof opts.count}`); | ||
if (!Number.isFinite(options.count)) { | ||
throw new TypeError(`Expected a finite number, got ${typeof options.count}`); | ||
} | ||
@@ -14,6 +15,24 @@ | ||
let elCount = 0; | ||
let maxErrors = -opts.count + 1; | ||
let maxFiltered = -opts.count + 1; | ||
let maxErrors = -options.count + 1; | ||
let maxFiltered = -options.count + 1; | ||
let done = false; | ||
const completed = new Set(); | ||
const cancelPendingIfDone = () => { | ||
if (!done) { | ||
return; | ||
} | ||
for (const promise of iterable) { | ||
if (!completed.has(promise) && typeof promise.cancel === 'function') { | ||
promise.cancel(); | ||
} | ||
} | ||
}; | ||
onCancel(() => { | ||
done = true; | ||
cancelPendingIfDone(); | ||
}); | ||
const fulfilled = value => { | ||
@@ -24,3 +43,3 @@ if (done) { | ||
if (typeof opts.filter === 'function' && !opts.filter(value)) { | ||
if (typeof options.filter === 'function' && !options.filter(value)) { | ||
if (--maxFiltered === 0) { | ||
@@ -36,3 +55,3 @@ done = true; | ||
if (--opts.count === 0) { | ||
if (--options.count === 0) { | ||
done = true; | ||
@@ -60,7 +79,19 @@ resolve(values); | ||
elCount++; | ||
Promise.resolve(el).then(fulfilled, rejected); | ||
Promise.resolve(el).then( | ||
value => { | ||
fulfilled(value); | ||
completed.add(el); | ||
cancelPendingIfDone(); | ||
}, | ||
error => { | ||
rejected(error); | ||
completed.add(el); | ||
cancelPendingIfDone(); | ||
} | ||
); | ||
} | ||
if (opts.count > elCount) { | ||
throw new RangeError(`Expected input to contain at least ${opts.count} items, but contains ${elCount} items`); | ||
if (options.count > elCount) { | ||
throw new RangeError(`Expected input to contain at least ${options.count} items, but contains ${elCount} items`); | ||
} | ||
@@ -67,0 +98,0 @@ }); |
{ | ||
"name": "p-some", | ||
"version": "2.0.1", | ||
"description": "Wait for a specified number of promises to be fulfilled", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-some", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"some", | ||
"resolved", | ||
"wait", | ||
"collection", | ||
"iterable", | ||
"iterator", | ||
"race", | ||
"fulfilled", | ||
"fastest", | ||
"async", | ||
"await", | ||
"promises", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"aggregate-error": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^2.0.0", | ||
"xo": "*" | ||
} | ||
"name": "p-some", | ||
"version": "3.0.0", | ||
"description": "Wait for a specified number of promises to be fulfilled", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-some", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"some", | ||
"resolved", | ||
"wait", | ||
"collection", | ||
"iterable", | ||
"iterator", | ||
"race", | ||
"fulfilled", | ||
"fastest", | ||
"async", | ||
"await", | ||
"promises", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"aggregate-error": "^1.0.0", | ||
"p-cancelable": "^0.4.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^3.0.0", | ||
"xo": "*" | ||
} | ||
} |
@@ -39,4 +39,6 @@ # p-some [![Build Status](https://travis-ci.org/sindresorhus/p-some.svg?branch=master)](https://travis-ci.org/sindresorhus/p-some) | ||
Returns a `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`](https://github.com/sindresorhus/aggregate-error) error. | ||
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`](https://github.com/sindresorhus/aggregate-error) 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 | ||
@@ -43,0 +45,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5625
78
76
2
+ Addedp-cancelable@^0.4.1
+ Addedp-cancelable@0.4.1(transitive)