Comparing version 1.0.0 to 1.1.0
46
index.js
'use strict'; | ||
module.exports = (iterable, mapper, opts) => new Promise((resolve, reject) => { | ||
const input = Array.from(iterable); | ||
if (input.length === 0) { | ||
resolve([]); | ||
return; | ||
} | ||
opts = Object.assign({ | ||
@@ -14,15 +7,13 @@ concurrency: Infinity | ||
let concurrency = opts.concurrency; | ||
const concurrency = opts.concurrency; | ||
if (concurrency === Infinity || concurrency > input.length) { | ||
concurrency = input.length; | ||
if (concurrency < 1) { | ||
throw new TypeError('Expected `concurrency` to be a number from 1 and up'); | ||
} | ||
if (!(Number.isFinite(concurrency) && concurrency >= 1)) { | ||
throw new TypeError('Expected `concurrency` to be a finite number from 1 and up'); | ||
} | ||
const ret = new Array(input.length); | ||
const ret = []; | ||
const iterator = iterable[Symbol.iterator](); | ||
let isRejected = false; | ||
let doneCount = 0; | ||
let iterableDone = false; | ||
let resolvingCount = 0; | ||
@@ -34,17 +25,22 @@ const next = i => { | ||
if (doneCount === input.length) { | ||
resolve(ret); | ||
return; | ||
} | ||
const nextItem = iterator.next(); | ||
if (i >= input.length) { | ||
if (nextItem.done) { | ||
iterableDone = true; | ||
if (resolvingCount === 0) { | ||
resolve(ret); | ||
} | ||
return; | ||
} | ||
Promise.resolve(input[i]) | ||
resolvingCount++; | ||
Promise.resolve(nextItem.value) | ||
.then(el => mapper(el, i)) | ||
.then( | ||
val => { | ||
doneCount++; | ||
ret[i] = val; | ||
resolvingCount--; | ||
next(i + concurrency); | ||
@@ -61,3 +57,7 @@ }, | ||
next(i); | ||
if (iterableDone) { | ||
break; | ||
} | ||
} | ||
}); |
{ | ||
"name": "p-map", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Map over promises concurrently", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -62,5 +62,6 @@ # p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map) | ||
Type: `number`<br> | ||
Default: `Infinity` | ||
Default: `Infinity`<br> | ||
Minimum: `1` | ||
Number of concurrent pending promises. | ||
Number of concurrently pending promises returned by `mapper`. | ||
@@ -72,4 +73,6 @@ | ||
- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently | ||
- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently | ||
- [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object` | ||
- [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially | ||
- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control | ||
- [More…](https://github.com/sindresorhus/promise-fun) | ||
@@ -76,0 +79,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
5179
82
48