diff --git a/.travis.yml b/.travis.yml index f98fed0..94ab01f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,3 @@ language: node_js node_js: - '12' - '10' - - '8' diff --git a/index.d.ts b/index.d.ts index 8f79629..e2e1d76 100644 --- a/index.d.ts +++ b/index.d.ts @@ -21,7 +21,7 @@ declare namespace pMap { @param element - Iterated element. @param index - Index of the element in the source array. */ - type Mapper = ( + type Mapper = ( element: Element, index: number ) => NewElement | Promise; diff --git a/index.js b/index.js index 5a8aeb2..e102015 100644 --- a/index.js +++ b/index.js @@ -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; @@ -32,7 +32,7 @@ module.exports = async ( } const nextItem = iterator.next(); - const i = currentIndex; + const index = currentIndex; currentIndex++; if (nextItem.done) { @@ -42,7 +42,7 @@ module.exports = async ( if (!stopOnError && errors.length !== 0) { reject(new AggregateError(errors)); } else { - resolve(ret); + resolve(result); } } @@ -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) { diff --git a/index.test-d.ts b/index.test-d.ts index a160384..c7da912 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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 => site; +const asyncSyncMapper = async (site: string, index: number): Promise => index > 1 ? site : Promise.resolve(site); -const multiResultTypeMapper = (site: string, index: number) => +const multiResultTypeMapper = async (site: string, index: number): Promise => index > 1 ? site.length : site; expectType(asyncMapper); @@ -35,7 +35,7 @@ expectType>(pMap(sites, asyncMapper)); expectType>(pMap(sites, asyncMapper, {concurrency: 2})); expectType>(pMap(sites, asyncSyncMapper)); -expectType>(pMap(sites, multiResultTypeMapper)); +expectType>>(pMap(sites, multiResultTypeMapper)); expectType>(pMap(sites, (site: string) => site)); expectType>(pMap(sites, (site: string) => site.length)); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index d19f5b3..18f7262 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/readme.md b/readme.md index b86e0f0..83ae32d 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -61,27 +59,25 @@ Type: `object` ##### concurrency -Type: `number`
-Default: `Infinity`
+Type: `number`\ +Default: `Infinity`\ Minimum: `1` Number of concurrently pending promises returned by `mapper`. ##### stopOnError -Type: `boolean`
+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