Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add aborted() utility function #46494

Merged
merged 26 commits into from Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/api/util.md
Expand Up @@ -1989,6 +1989,35 @@ const channel = new MessageChannel();
channel.port2.postMessage(signal, [signal]);
```

## `util.aborted(signal, resource)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* `signal` {AbortSignal}
* `resource` {any} Any non-null entity
jasnell marked this conversation as resolved.
Show resolved Hide resolved
* Returns: {Promise}

Listens to abort event on the provided `signal` and returns a promise which is
resolved if an abort event is triggered. The function cleans up any
event listeners depending on when the provided `resource` is garbage collected.

```js
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
const { aborted } = require('util');
debadree25 marked this conversation as resolved.
Show resolved Hide resolved

const ac = new AbortController();
const dependent = {};
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

aborted(ac.signal, dependent).then(() => {
// Do something when the abort event is triggered.
});

doSomething(dependent, ac.signal); // Can trigger the abort event
```

## `util.types`

<!-- YAML
Expand Down
11 changes: 4 additions & 7 deletions lib/internal/abort_controller.js
Expand Up @@ -14,7 +14,6 @@ const {
SymbolToStringTag,
WeakRef,
PromiseResolve,
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
PromiseReject,
} = primordials;

const {
Expand Down Expand Up @@ -44,6 +43,7 @@ const {
const {
validateUint32,
validateAbortSignal: abortSignalValidator,
validateObject,
} = require('internal/validators');

const {
Expand Down Expand Up @@ -367,12 +367,9 @@ function transferableAbortController() {
* @param {any} resource
* @returns {Promise<void>}
*/
function aborted(signal, resource = null) {
try {
abortSignalValidator(signal, 'signal');
} catch (err) {
return PromiseReject(err);
}
async function aborted(signal, resource) {
abortSignalValidator(signal, 'signal');
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
validateObject(resource, 'resource', { nullable: false, allowFunction: true, allowArray: true });
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
if (signal.aborted)
return PromiseResolve();
const abortPromise = createDeferredPromise();
Expand Down
19 changes: 9 additions & 10 deletions test/parallel/test-aborted-util.js
Expand Up @@ -6,15 +6,6 @@ const { aborted } = require('util');
const assert = require('assert');
const { getEventListeners } = require('events');

{
// Test aborted works
const ac = new AbortController();
aborted(ac.signal).then(common.mustCall());
ac.abort();
assert.strictEqual(ac.signal.aborted, true);
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
}

{
// Test aborted works when provided a resource
const ac = new AbortController();
Expand All @@ -39,7 +30,15 @@ const { getEventListeners } = require('events');
{
// Fails with error if not provided abort signal
const sig = new EventTarget();
assert.rejects(aborted(sig), {
assert.rejects(aborted(sig, {}), {
name: 'TypeError',
});
}

{
// Fails if not provided a resource
const ac = new AbortController();
assert.rejects(aborted(ac.signal, null), {
name: 'TypeError',
});
}
debadree25 marked this conversation as resolved.
Show resolved Hide resolved