p-map
Advanced tools
Comparing version 1.2.0 to 2.0.0
34
index.js
'use strict'; | ||
module.exports = (iterable, mapper, opts) => new Promise((resolve, reject) => { | ||
opts = Object.assign({ | ||
const pMap = (iterable, mapper, options) => new Promise((resolve, reject) => { | ||
options = Object.assign({ | ||
concurrency: Infinity | ||
}, opts); | ||
}, options); | ||
@@ -11,3 +12,3 @@ if (typeof mapper !== 'function') { | ||
const concurrency = opts.concurrency; | ||
const {concurrency} = options; | ||
@@ -21,5 +22,5 @@ if (!(typeof concurrency === 'number' && concurrency >= 1)) { | ||
let isRejected = false; | ||
let iterableDone = false; | ||
let isIterableDone = false; | ||
let resolvingCount = 0; | ||
let currentIdx = 0; | ||
let currentIndex = 0; | ||
@@ -32,7 +33,7 @@ const next = () => { | ||
const nextItem = iterator.next(); | ||
const i = currentIdx; | ||
currentIdx++; | ||
const i = currentIndex; | ||
currentIndex++; | ||
if (nextItem.done) { | ||
iterableDone = true; | ||
isIterableDone = true; | ||
@@ -49,12 +50,12 @@ if (resolvingCount === 0) { | ||
Promise.resolve(nextItem.value) | ||
.then(el => mapper(el, i)) | ||
.then(element => mapper(element, i)) | ||
.then( | ||
val => { | ||
ret[i] = val; | ||
value => { | ||
ret[i] = value; | ||
resolvingCount--; | ||
next(); | ||
}, | ||
err => { | ||
error => { | ||
isRejected = true; | ||
reject(err); | ||
reject(error); | ||
} | ||
@@ -67,3 +68,3 @@ ); | ||
if (iterableDone) { | ||
if (isIterableDone) { | ||
break; | ||
@@ -73,1 +74,4 @@ } | ||
}); | ||
module.exports = pMap; | ||
module.exports.default = pMap; |
{ | ||
"name": "p-map", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Map over promises concurrently", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -42,8 +43,9 @@ "keywords": [ | ||
"ava": "*", | ||
"delay": "^2.0.0", | ||
"delay": "^3.0.0", | ||
"in-range": "^1.0.0", | ||
"random-int": "^1.0.0", | ||
"time-span": "^2.0.0", | ||
"tsd-check": "^0.2.1", | ||
"xo": "*" | ||
} | ||
} |
@@ -28,11 +28,15 @@ # p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map) | ||
const mapper = el => got.head(el).then(res => res.requestUrl); | ||
(async () => { | ||
const mapper = async site => { | ||
const {requestUrl} = await got.head(site); | ||
return requestUrl; | ||
}; | ||
pMap(sites, mapper, {concurrency: 2}).then(result => { | ||
const result = await pMap(sites, mapper, {concurrency: 2}); | ||
console.log(result); | ||
//=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/'] | ||
}); | ||
})(); | ||
``` | ||
## API | ||
@@ -39,0 +43,0 @@ |
6943
5
96
86
7