Skip to content

Commit

Permalink
Support AbortController (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
jopemachine and sindresorhus authored May 17, 2022
1 parent 735d80e commit 4875dee
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ export interface Options {
@default true
*/
readonly stopOnError?: boolean;

/**
You can abort the promises using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
**Requires Node.js 16 or later.*
@example
```
import pMap from 'p-map';
import delay from 'delay';
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, 500);
const mapper = async value => value;
await pMap([delay(1000), delay(1000)], mapper, {signal: abortController.signal});
// Throws AbortError (DOMException) after 500 ms.
```
*/
readonly signal?: AbortSignal;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
import AggregateError from 'aggregate-error';

/**
An error to be thrown when the request is aborted by AbortController.
DOMException is thrown instead of this Error when DOMException is available.
*/
export class AbortError extends Error {
constructor(message) {
super();
this.name = 'AbortError';
this.message = message;
}
}

/**
TODO: Remove AbortError and just throw DOMException when targeting Node 18.
*/
const getDOMException = errorMessage => globalThis.DOMException === undefined
? new AbortError(errorMessage)
: new DOMException(errorMessage);

/**
TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.
*/
const getAbortedReason = signal => {
const reason = signal.reason === undefined
? getDOMException('This operation was aborted.')
: signal.reason;

return reason instanceof Error ? reason : getDOMException(reason);
};

export default async function pMap(
iterable,
mapper,
{
concurrency = Number.POSITIVE_INFINITY,
stopOnError = true,
signal,
} = {},
) {
return new Promise((resolve, reject_) => {
Expand Down Expand Up @@ -37,6 +68,16 @@ export default async function pMap(
reject_(reason);
};

if (signal) {
if (signal.aborted) {
reject(getAbortedReason(signal));
}

signal.addEventListener('abort', () => {
reject(getAbortedReason(signal));
});
}

const next = async () => {
if (isResolved) {
return;
Expand Down
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ When `false`, instead of stopping when a promise rejects, it will wait for all t

Caveat: When `true`, any already-started async mappers will continue to run until they resolve or reject. In the case of infinite concurrency with sync iterables, *all* mappers are invoked on startup and will continue after the first rejection. [Issue #51](https://github.com/sindresorhus/p-map/issues/51) can be implemented for abort control.

##### signal

Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)

You can abort the promises using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).

*Requires Node.js 16 or later.*

```js
import pMap from 'p-map';
import delay from 'delay';

const abortController = new AbortController();

setTimeout(() => {
abortController.abort();
}, 500);

const mapper = async value => value;

await pMap([delay(1000), delay(1000)], mapper, {signal: abortController.signal});
// Throws AbortError (DOMException) after 500 ms.
```

### pMapSkip

Return this value from a `mapper` function to skip including the value in the returned array.
Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,31 @@ test('no unhandled rejected promises from mapper throws - concurrency 1', async
test('invalid mapper', async t => {
await t.throwsAsync(pMap([], 'invalid mapper', {concurrency: 2}), {instanceOf: TypeError});
});

if (globalThis.AbortController !== undefined) {
test('abort by AbortController', async t => {
const abortController = new AbortController();

setTimeout(() => {
abortController.abort();
}, 100);

const mapper = async value => value;

await t.throwsAsync(pMap([delay(1000), new AsyncTestData(100), 100], mapper, {signal: abortController.signal}), {
name: 'AbortError',
});
});

test('already aborted signal', async t => {
const abortController = new AbortController();

abortController.abort();

const mapper = async value => value;

await t.throwsAsync(pMap([delay(1000), new AsyncTestData(100), 100], mapper, {signal: abortController.signal}), {
name: 'AbortError',
});
});
}

0 comments on commit 4875dee

Please sign in to comment.