Skip to content

Commit

Permalink
Remove p-try dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 25, 2020
1 parent 54c0ba8 commit 69b6017
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 28 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

20 changes: 10 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
declare namespace pLimit {
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.
*/
Expand All @@ -28,6 +18,16 @@ declare namespace pLimit {
Note: This does not cancel promises that are already running.
*/
clearQueue: () => void;

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

Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const pTry = require('p-try');
const Queue = require('yocto-queue');

const pLimit = concurrency => {
Expand All @@ -21,8 +20,7 @@ const pLimit = concurrency => {
const run = async (fn, resolve, ...args) => {
activeCount++;

// TODO: Get rid of `pTry`. It's not needed anymore.
const result = pTry(fn, ...args);
const result = (async () => fn(...args))();

resolve(result);

Expand All @@ -49,7 +47,10 @@ const pLimit = concurrency => {
})();
};

const generator = (fn, ...args) => new Promise(resolve => enqueue(fn, resolve, ...args));
const generator = (fn, ...args) => new Promise(resolve => {
enqueue(fn, resolve, ...args);
});

Object.defineProperties(generator, {
activeCount: {
get: () => activeCount
Expand Down
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import pLimit = require('.');
const limit = pLimit(1);

const input = [
limit(() => Promise.resolve('foo')),
limit(() => Promise.resolve('bar')),
limit(() => Promise.resolve(undefined))
limit(async () => 'foo'),
limit(async () => 'bar'),
limit(async () => 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<Promise<string>>(limit(async (a: string, b: number) => '', 'test', 1));

expectType<number>(limit.activeCount);
expectType<number>(limit.pendingCount);
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@
"bluebird"
],
"dependencies": {
"p-try": "^2.0.0",
"yocto-queue": "^0.1.0"
},
"devDependencies": {
"ava": "^2.4.0",
"delay": "^4.1.0",
"delay": "^4.4.0",
"in-range": "^2.0.0",
"random-int": "^2.0.1",
"time-span": "^4.0.0",
"tsd": "^0.11.0",
"xo": "^0.26.0"
"tsd": "^0.13.1",
"xo": "^0.35.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# p-limit [![Build Status](https://travis-ci.com/sindresorhus/p-limit.svg?branch=master)](https://travis-ci.com/github/sindresorhus/p-limit)
# p-limit

> Run multiple promise-returning & async functions with limited concurrency
Expand Down

0 comments on commit 69b6017

Please sign in to comment.