Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 5, 2020
1 parent f8ccb4e commit bf03769
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 22 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -21,7 +21,7 @@ declare namespace pMap {
@param element - Iterated element.
@param index - Index of the element in the source array.
*/
type Mapper<Element = any, NewElement = any> = (
type Mapper<Element = any, NewElement = unknown> = (
element: Element,
index: number
) => NewElement | Promise<NewElement>;
Expand Down
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -18,7 +18,7 @@ module.exports = async (
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`);
}

const ret = [];
const result = [];
const errors = [];
const iterator = iterable[Symbol.iterator]();
let isRejected = false;
Expand All @@ -32,7 +32,7 @@ module.exports = async (
}

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

if (nextItem.done) {
Expand All @@ -42,7 +42,7 @@ module.exports = async (
if (!stopOnError && errors.length !== 0) {
reject(new AggregateError(errors));
} else {
resolve(ret);
resolve(result);
}
}

Expand All @@ -54,7 +54,7 @@ module.exports = async (
(async () => {
try {
const element = await nextItem.value;
ret[i] = await mapper(element, i);
result[index] = await mapper(element, index);
resolvingCount--;
next();
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions index.test-d.ts
Expand Up @@ -14,10 +14,10 @@ const numbers = [
2
];

const asyncMapper = async (site: string) => site;
const asyncSyncMapper = (site: string, index: number) =>
const asyncMapper = async (site: string): Promise<string> => site;
const asyncSyncMapper = async (site: string, index: number): Promise<string> =>
index > 1 ? site : Promise.resolve(site);
const multiResultTypeMapper = (site: string, index: number) =>
const multiResultTypeMapper = async (site: string, index: number): Promise<string | number> =>
index > 1 ? site.length : site;

expectType<Mapper>(asyncMapper);
Expand All @@ -35,7 +35,7 @@ expectType<Promise<string[]>>(pMap(sites, asyncMapper));
expectType<Promise<string[]>>(pMap(sites, asyncMapper, {concurrency: 2}));

expectType<Promise<string[]>>(pMap(sites, asyncSyncMapper));
expectType<Promise<(string | number)[]>>(pMap(sites, multiResultTypeMapper));
expectType<Promise<Array<string | number>>>(pMap(sites, multiResultTypeMapper));

expectType<Promise<string[]>>(pMap(sites, (site: string) => site));
expectType<Promise<number[]>>(pMap(sites, (site: string) => site.length));
Expand Down
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -4,13 +4,14 @@
"description": "Map over promises concurrently",
"license": "MIT",
"repository": "sindresorhus/p-map",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -46,7 +47,7 @@
"in-range": "^2.0.0",
"random-int": "^2.0.0",
"time-span": "^3.1.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"tsd": "^0.7.4",
"xo": "^0.27.2"
}
}
10 changes: 3 additions & 7 deletions readme.md
Expand Up @@ -4,14 +4,12 @@
Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently.


## Install

```
$ npm install p-map
```


## Usage

```js
Expand Down Expand Up @@ -61,27 +59,25 @@ Type: `object`

##### concurrency

Type: `number`<br>
Default: `Infinity`<br>
Type: `number`\
Default: `Infinity`\
Minimum: `1`

Number of concurrently pending promises returned by `mapper`.

##### stopOnError

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

When set to `false`, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an [aggregated error](https://github.com/sindresorhus/aggregate-error) containing all the errors from the rejected promises.


## p-map for enterprise

Available as part of the Tidelift Subscription.

The maintainers of p-map and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-p-map?utm_source=npm-p-map&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)


## Related

- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
Expand Down

0 comments on commit bf03769

Please sign in to comment.