diff --git a/types/p-limit/index.d.ts b/types/p-limit/index.d.ts index af0306cdd9..de7b0a07d9 100644 --- a/types/p-limit/index.d.ts +++ b/types/p-limit/index.d.ts @@ -1,18 +1,43 @@ -// Type definitions for p-limit 2.0 +// Type definitions for p-limit 2.1 // Project: https://github.com/sindresorhus/p-limit#readme // Definitions by: BendingBender // Linus Unnebäck // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 export = pLimit; -declare function limit(cb: (a: A, b: B, c: C, d: D, e: E, f: F, ...args: any[]) => PromiseLike | T, a: A, b: B, c: C, d: D, e: E, f: F, ...args: any[]): Promise; -declare function limit(cb: (a: A, b: B, c: C, d: D, e: E, f: F) => PromiseLike | T, a: A, b: B, c: C, d: D, e: E, f: F): Promise; -declare function limit(cb: (a: A, b: B, c: C, d: D, e: E) => PromiseLike | T, a: A, b: B, c: C, d: D, e: E): Promise; -declare function limit(cb: (a: A, b: B, c: C, d: D) => PromiseLike | T, a: A, b: B, c: C, d: D): Promise; -declare function limit(cb: (a: A, b: B, c: C) => PromiseLike | T, a: A, b: B, c: C): Promise; -declare function limit(cb: (a: A, b: B) => PromiseLike | T, a: A, b: B): Promise; -declare function limit(cb: (a: A) => PromiseLike | T, a: A): Promise; -declare function limit(cb: () => PromiseLike | T): Promise; +/** + * Run multiple promise-returning & async functions with limited concurrency. + * @param concurrency Concurrency limit. Minimum: `1`. + * @returns A `limit` function. + */ +declare function pLimit(concurrency: number): pLimit.Limit; -declare function pLimit(concurrency: number): typeof limit; +declare namespace pLimit { + interface Limit { + /** + * Returns the promise returned by calling `fn(...args)`. + * + * @param fn Promise-returning/async function. + * @param args Any arguments to pass through to `fn`. + * Support for passing arguments on to the `fn` is provided in order to be able to avoid + * creating unnecessary closures. You probably don't need this optimization unless you're + * pushing a lot of functions. + */ + ( + fn: (...args: TArgs) => PromiseLike | R, + ...args: TArgs + ): Promise; + + /** + * The number of promises that are currently running. + */ + readonly activeCount: number; + + /** + * The number of promises that are waiting to run (i.e. their internal `fn` was not called yet). + */ + readonly pendingCount: number; + } +} diff --git a/types/p-limit/p-limit-tests.ts b/types/p-limit/p-limit-tests.ts index 51dcb394fe..3f9fb51083 100644 --- a/types/p-limit/p-limit-tests.ts +++ b/types/p-limit/p-limit-tests.ts @@ -8,28 +8,16 @@ const input = [ limit(() => Promise.resolve(undefined)), ]; -Promise.all(input).then(result => { - const str: string | undefined = result[0]; +Promise.all(input); // $ExpectType Promise<(string | undefined)[]> + +limit((a: string) => '', 'test').then(v => { + v; // $ExpectType string +}); +limit((a: string, b: number) => Promise.resolve(''), 'test', 1).then(v => { + v; // $ExpectType string }); -let str: string; - -declare function a(a: string): string; -declare function b(a: string, b: number): string; -declare function c(a: string, b: number, c: boolean): string; -declare function d(a: string, b: number, c: boolean, d: symbol): string; -declare function e(a: string, b: number, c: boolean, d: symbol, e: 'yes' | 'no'): string; -declare function f(a: string, b: number, c: boolean, d: symbol, e: 'yes' | 'no', f: 1 | 2): string; -declare function g(a: string, b: number, c: boolean, d: symbol, e: 'yes' | 'no', f: 1 | 2, g: true): string; - -limit(a, 'test').then(v => { str = v; }); -limit(b, 'test', 1).then(v => { str = v; }); -limit(c, 'test', 1, false).then(v => { str = v; }); -limit(d, 'test', 1, false, Symbol('test')).then(v => { str = v; }); -limit(e, 'test', 1, false, Symbol('test'), 'no').then(v => { str = v; }); -limit(f, 'test', 1, false, Symbol('test'), 'no', 2).then(v => { str = v; }); -limit(g, 'test', 1, false, Symbol('test'), 'no', 2, true).then(v => { str = v; }); - -declare function add(...args: number[]): number; - -limit(add, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13).then(v => (v === 91)); +limit.activeCount; // $ExpectType number +limit.activeCount = 1; // $ExpectError +limit.pendingCount; // $ExpectType number +limit.pendingCount = 1; // $ExpectError