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
Changes from 1 commit
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
39 changes: 27 additions & 12 deletions doc/api/util.md
Expand Up @@ -1998,27 +1998,42 @@ added: REPLACEME
> Stability: 1 - Experimental

* `signal` {AbortSignal}
* `resource` {any} Any non-null entity
* `resource` {Object} Any non-null entity, reference to which is held weakly.
* 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 is dependent on the `resource`
entity and will be resolved only if the `resource` is not removed
from memory, when `resource` goes out of memory the
promise shall remain pending and any event listeners attached to the `signal`
will be removed.
Listens to abort event on the provided `signal` and
returns a promise that resolves when the `signal` is
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
aborted. The function is dependent on the `resource` and
the returned promise shall remain pending if `resource`
goes out of memory. The function shall automatically
cleanup any event listeners it attaches to `signal`.
debadree25 marked this conversation as resolved.
Show resolved Hide resolved

```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 = {};
const dependent = obtainSomethingAbortable();

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

doSomething(dependent, ac.signal); // Can trigger the abort event
dependent.on('event', () => {
dependent.abort();
});
```

```mjs
import { aborted } from 'node:util';

const dependent = obtainSomethingAbortable();

aborted(dependent.signal, dependent).then(() => {
// Do something when dependent is aborted.
});

dependent.on('event', () => {
dependent.abort();
});
```

## `util.types`
Expand Down