Skip to content

Commit

Permalink
Add support for clearing the queue (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
callmehiphop and sindresorhus authored Apr 5, 2020
1 parent 6d7a225 commit d91b326
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export interface Limit {
The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
*/
readonly pendingCount: number;

/**
Discard pending promises that are waiting to run.
This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
Note: This does not cancel promises that are already running.
*/
clearQueue(): void;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const pLimit = concurrency => {
},
pendingCount: {
get: () => queue.length
},
clearQueue: {
value: () => {
queue.length = 0;
}
}
});

Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ expectType<Promise<string>>(limit((a: string, b: number) => Promise.resolve(''),

expectType<number>(limit.activeCount);
expectType<number>(limit.pendingCount);

expectType<void>(limit.clearQueue());
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ The number of promises that are currently running.

The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).

### limit.clearQueue()

Discard pending promises that are waiting to run.

This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.

Note: This does not cancel promises that are already running.

## FAQ

### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package?
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ test('activeCount and pendingCount properties', async t => {
t.is(limit.pendingCount, 0);
});

test('clearQueue', t => {
const limit = pLimit(1);

Array.from({length: 1}, () => limit(() => delay(1000)));
Array.from({length: 3}, () => limit(() => delay(1000)));

t.is(limit.pendingCount, 3);
limit.clearQueue();
t.is(limit.pendingCount, 0);
});

test('throws on invalid concurrency argument', async t => {
await t.throwsAsync(pLimit(0));
await t.throwsAsync(pLimit(-1));
Expand Down

0 comments on commit d91b326

Please sign in to comment.