Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 6, add TypeScript definition, update dependencies #4

Merged
merged 2 commits into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
188 changes: 188 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
export interface Options {
/**
* Number of concurrent pending promises. Minimum: `1`.
*
* @default Infinity
*/
concurrency?: number;
}

export type PromiseFactory<T> = () => PromiseLike<T>;

/**
* 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are more than 10 inputs? There must be a better way of achieving this...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a not-so-convenient fallback for any number of parameters:

declare function pAll<TAll>(
	tasks: Iterable<PromiseFactory<TAll>> | PromiseFactory<TAll>[],
	options?: Options
): Promise<TAll[]>;

ATM, TypeScript doesn't have a better way to describe this one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if there's any TS issue for this? Seems like a very big limitation.

Copy link
Contributor

@privatenumber privatenumber May 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you've already found it in sindresorhus/p-progress#10, but for anyone else that's reading this there's a related issue in microsoft/TypeScript#5453, which was addressed via variadic tuple types, but unclear whether it solves this yet

Experimental PR to add variadic tuple to Promise.all microsoft/TypeScript#39796

>(
tasks: [
PromiseFactory<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>,
PromiseFactory<Result5>,
PromiseFactory<Result6>,
PromiseFactory<Result7>,
PromiseFactory<Result8>,
PromiseFactory<Result9>,
PromiseFactory<Result10>
],
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<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>,
PromiseFactory<Result5>,
PromiseFactory<Result6>,
PromiseFactory<Result7>,
PromiseFactory<Result8>,
PromiseFactory<Result9>
],
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<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>,
PromiseFactory<Result5>,
PromiseFactory<Result6>,
PromiseFactory<Result7>,
PromiseFactory<Result8>
],
options?: Options
): Promise<
[Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8]
>;
declare function pAll<
Result1,
Result2,
Result3,
Result4,
Result5,
Result6,
Result7
>(
tasks: [
PromiseFactory<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>,
PromiseFactory<Result5>,
PromiseFactory<Result6>,
PromiseFactory<Result7>
],
options?: Options
): Promise<[Result1, Result2, Result3, Result4, Result5, Result6, Result7]>;
declare function pAll<Result1, Result2, Result3, Result4, Result5, Result6>(
tasks: [
PromiseFactory<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>,
PromiseFactory<Result5>,
PromiseFactory<Result6>
],
options?: Options
): Promise<[Result1, Result2, Result3, Result4, Result5, Result6]>;
declare function pAll<Result1, Result2, Result3, Result4, Result5>(
tasks: [
PromiseFactory<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>,
PromiseFactory<Result5>
],
options?: Options
): Promise<[Result1, Result2, Result3, Result4, Result5]>;
declare function pAll<Result1, Result2, Result3, Result4>(
tasks: [
PromiseFactory<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>,
PromiseFactory<Result4>
],
options?: Options
): Promise<[Result1, Result2, Result3, Result4]>;
declare function pAll<Result1, Result2, Result3>(
tasks: [
PromiseFactory<Result1>,
PromiseFactory<Result2>,
PromiseFactory<Result3>
],
options?: Options
): Promise<[Result1, Result2, Result3]>;
declare function pAll<Result1, Result2>(
tasks: [PromiseFactory<Result1>, PromiseFactory<Result2>],
options?: Options
): Promise<[Result1, Result2]>;
declare function pAll<Result1>(
tasks: [PromiseFactory<Result1>],
options?: Options
): Promise<[Result1]>;
declare function pAll<TAll>(
tasks: Iterable<PromiseFactory<TAll>> | PromiseFactory<TAll>[],
options?: Options
): Promise<TAll[]>;

export default pAll;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
const pMap = require('p-map');

module.exports = (iterable, opts) => pMap(iterable, el => el(), opts);
module.exports.default = module.exports;
21 changes: 21 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {expectType} from 'tsd-check';
import pAll from '.';

const actions: [
() => Promise<string>,
() => Promise<string>,
() => Promise<void>,
() => Promise<number>
] = [
() => Promise.resolve('sindresorhus.com'),
() => Promise.resolve('ava.li'),
() => Promise.resolve(),
() => Promise.resolve(1)
];

const result = await pAll(actions, {concurrency: 2});

expectType<string>(result[0]);
expectType<string>(result[1]);
expectType<void>(result[2]);
expectType<number>(result[3]);
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down