This is not GitHub.com

Like what you see? Install OctoLinker now!

Skip to content
Permalink
Browse files

Require Node.js 6

  • Loading branch information
sindresorhus committed Jul 28, 2018
1 parent 386b340 commit fcfcf9192dfe462072184897262628b3e2fe9db6
Showing with 28 additions and 51 deletions.
  1. +1 −2 .gitattributes
  2. +1 −1 .travis.yml
  3. +15 −15 index.js
  4. +2 −2 package.json
  5. +6 −28 readme.md
  6. +3 −3 test.js
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
@@ -1,14 +1,14 @@
'use strict';
module.exports = (iterable, mapper, opts) => new Promise((resolve, reject) => {
opts = Object.assign({
module.exports = (iterable, mapper, options) => new Promise((resolve, reject) => {
options = Object.assign({
concurrency: Infinity
}, opts);
}, options);

if (typeof mapper !== 'function') {
throw new TypeError('Mapper function is required');
}

const concurrency = opts.concurrency;
const {concurrency} = options;

if (!(typeof concurrency === 'number' && concurrency >= 1)) {
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`);
@@ -17,21 +17,21 @@ module.exports = (iterable, mapper, opts) => new Promise((resolve, reject) => {
const ret = [];
const iterator = iterable[Symbol.iterator]();
let isRejected = false;
let iterableDone = false;
let isIterableDone = false;
let resolvingCount = 0;
let currentIdx = 0;
let currentIndex = 0;

const next = () => {
if (isRejected) {
return;
}

const nextItem = iterator.next();
const i = currentIdx;
currentIdx++;
const i = currentIndex;
currentIndex++;

if (nextItem.done) {
iterableDone = true;
isIterableDone = true;

if (resolvingCount === 0) {
resolve(ret);
@@ -43,24 +43,24 @@ module.exports = (iterable, mapper, opts) => new Promise((resolve, reject) => {
resolvingCount++;

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);
}
);
};

for (let i = 0; i < concurrency; i++) {
next();

if (iterableDone) {
if (isIterableDone) {
break;
}
}
@@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
@@ -38,7 +38,7 @@
],
"devDependencies": {
"ava": "*",
"delay": "^2.0.0",
"delay": "^3.0.0",
"in-range": "^1.0.0",
"random-int": "^1.0.0",
"time-span": "^2.0.0",
@@ -14,8 +14,6 @@ $ npm install p-map

## Usage

### Promises

```js
const pMap = require('p-map');
const got = require('got');
@@ -27,37 +25,17 @@ const sites = [
'github.com'
];
const mapper = el => got.head(el).then(res => res.requestUrl);
pMap(sites, mapper, {concurrency: 2}).then(result => {
console.log(result);
//=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/']
});
```

### Async/Await

```js
const pMap = require('p-map');
const got = require('got');
const sites = [
getWebsiteFromUsername('sindresorhus'), //=> Promise
'ava.li',
'todomvc.com',
'github.com'
];
const run = async () => {
const mapper = async (el) => {
const { requestUrl } = await got.head(el);
(async () => {
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await pMap(sites, mapper, {concurrency: 2});
console.log(result);
//=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/']
};
run();
})();
```

## API
@@ -3,7 +3,7 @@ import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import m from './';
import m from '.';

const input = [
Promise.resolve([10, 300]),
@@ -29,7 +29,7 @@ test('concurrency: 4', async t => {
const concurrency = 4;
let running = 0;

await m(Array(100).fill(0), async () => {
await m(new Array(100).fill(0), async () => {
running++;
t.true(running <= concurrency);
await delay(randomInt(30, 200));
@@ -42,7 +42,7 @@ test('handles empty iterable', async t => {
});

test('async with concurrency: 2 (random time sequence)', async t => {
const input = Array(10).map(() => randomInt(0, 100));
const input = new Array(10).map(() => randomInt(0, 100));
const mapper = value => delay(value).then(() => value);
const result = await m(input, mapper, {concurrency: 2});
t.deepEqual(result, input);

0 comments on commit fcfcf91

Please sign in to comment.
You can’t perform that action at this time.