From b49062c9afff0e64a6f6b9ea21fb0f5bc654a978 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 25 Feb 2019 23:38:30 +0100 Subject: [PATCH 1/2] Require Node.js 6, add TypeScript definition, update dependencies --- .editorconfig | 2 +- .gitattributes | 3 +- .gitignore | 2 + .npmrc | 1 + .travis.yml | 3 +- index.d.ts | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 1 + index.test-d.ts | 21 ++++++ package.json | 14 ++-- test.js | 2 +- 10 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..7c4e91c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +yarn.lock +.prettierrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index 97519af..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: + - '10' + - '8' - '6' - - '4' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..651d386 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,188 @@ +export interface Options { + /** + * Number of concurrent pending promises. Minimum: `1`. + * + * @default Infinity + */ + concurrency?: number; +} + +export type PromiseFactory = () => PromiseLike; + +/** + * Run promise-returning & async functions concurrently with optional limited concurrency. + * + * @param tasks - Iterable with promise-returning/async functions. + * @returns A `Promise` that is fulfilled when all promises returned from calling the functions in `tasks` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values in `tasks` order. + */ +declare function pAll< + Result1, + Result2, + Result3, + Result4, + Result5, + Result6, + Result7, + Result8, + Result9, + Result10 +>( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise< + [ + Result1, + Result2, + Result3, + Result4, + Result5, + Result6, + Result7, + Result8, + Result9, + Result10 + ] +>; +declare function pAll< + Result1, + Result2, + Result3, + Result4, + Result5, + Result6, + Result7, + Result8, + Result9 +>( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise< + [ + Result1, + Result2, + Result3, + Result4, + Result5, + Result6, + Result7, + Result8, + Result9 + ] +>; +declare function pAll< + Result1, + Result2, + Result3, + Result4, + Result5, + Result6, + Result7, + Result8 +>( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise< + [Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8] +>; +declare function pAll< + Result1, + Result2, + Result3, + Result4, + Result5, + Result6, + Result7 +>( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise<[Result1, Result2, Result3, Result4, Result5, Result6, Result7]>; +declare function pAll( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise<[Result1, Result2, Result3, Result4, Result5, Result6]>; +declare function pAll( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise<[Result1, Result2, Result3, Result4, Result5]>; +declare function pAll( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise<[Result1, Result2, Result3, Result4]>; +declare function pAll( + tasks: [ + PromiseFactory, + PromiseFactory, + PromiseFactory + ], + options?: Options +): Promise<[Result1, Result2, Result3]>; +declare function pAll( + tasks: [PromiseFactory, PromiseFactory], + options?: Options +): Promise<[Result1, Result2]>; +declare function pAll( + tasks: [PromiseFactory], + options?: Options +): Promise<[Result1]>; +declare function pAll( + tasks: Iterable> | PromiseFactory[], + options?: Options +): Promise; + +export default pAll; diff --git a/index.js b/index.js index 2c9661c..53bbd22 100644 --- a/index.js +++ b/index.js @@ -2,3 +2,4 @@ const pMap = require('p-map'); module.exports = (iterable, opts) => pMap(iterable, el => el(), opts); +module.exports.default = module.exports; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..1f905fa --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,21 @@ +import {expectType} from 'tsd-check'; +import pAll from '.'; + +const actions: [ + () => Promise, + () => Promise, + () => Promise, + () => Promise +] = [ + () => Promise.resolve('sindresorhus.com'), + () => Promise.resolve('ava.li'), + () => Promise.resolve(), + () => Promise.resolve(1) +]; + +const result = await pAll(actions, {concurrency: 2}); + +expectType(result[0]); +expectType(result[1]); +expectType(result[2]); +expectType(result[3]); diff --git a/package.json b/package.json index 200610e..49ecfee 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,14 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=4" + "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd-check" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "promise", @@ -42,12 +43,13 @@ "bluebird" ], "dependencies": { - "p-map": "^1.0.0" + "p-map": "^2.0.0" }, "devDependencies": { - "ava": "*", + "ava": "^1.2.1", "delay": "^1.3.1", - "xo": "*" + "tsd-check": "^0.3.0", + "xo": "^0.24.0" }, "xo": { "esnext": true diff --git a/test.js b/test.js index 9d925fd..6a311a0 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ import test from 'ava'; import delay from 'delay'; -import m from './'; +import m from '.'; // See `p-map` for more comprehensive tests test('main', async t => { From df288c523a7e134235252f427edb86e8c4fa99d2 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 28 Feb 2019 17:14:24 +0100 Subject: [PATCH 2/2] remove .prettierrc from .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7c4e91c..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ node_modules yarn.lock -.prettierrc