Skip to content

Commit

Permalink
Add TypeScript definition (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 2, 2019
1 parent 8517860 commit 7152662
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
29 changes: 29 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export interface Limit {
/**
* @param fn - Promise-returning/async function.
* @param arguments - 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.
* @returns The promise returned by calling `fn(...arguments)`.
*/
<Arguments extends unknown[], ReturnType>(
fn: (...arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
...arguments: Arguments
): Promise<ReturnType>;

/**
* 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;
}

/**
* Run multiple promise-returning & async functions with limited concurrency.
*
* @param concurrency - Concurrency limit. Minimum: `1`.
* @returns A `limit` function.
*/
export default function pLimit(concurrency: number): Limit;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const pTry = require('p-try');

module.exports = concurrency => {
const pLimit = concurrency => {
if (concurrency < 1) {
throw new TypeError('Expected `concurrency` to be a number from 1 and up');
}
Expand Down Expand Up @@ -47,3 +47,6 @@ module.exports = concurrency => {

return generator;
};

module.exports = pLimit;
module.exports.default = pLimit;
18 changes: 18 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {expectType} from 'tsd-check';
import pLimit from '.';

const limit = pLimit(1);

const input = [
limit(() => Promise.resolve('foo')),
limit(() => Promise.resolve('bar')),
limit(() => Promise.resolve(undefined)),
];

expectType<Promise<Array<string | undefined>>>(Promise.all(input));

expectType<Promise<string>>(limit((a: string) => '', 'test'));
expectType<Promise<string>>(limit((a: string, b: number) => Promise.resolve(''), 'test', 1));

expectType<number>(limit.activeCount);
expectType<number>(limit.pendingCount);
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
Expand Down Expand Up @@ -44,6 +45,7 @@
"in-range": "^1.0.0",
"random-int": "^1.0.0",
"time-span": "^2.0.0",
"tsd-check": "^0.3.0",
"xo": "^0.23.0"
}
}

0 comments on commit 7152662

Please sign in to comment.