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

doc: replace outdated util.promisify timer examples with references to new awaitable timers #39164

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 10 additions & 35 deletions doc/api/timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,7 @@ next event loop iteration.
If `callback` is not a function, a [`TypeError`][] will be thrown.

This method has a custom variant for promises that is available using
[`util.promisify()`][]:

```js
const util = require('util');
const setImmediatePromise = util.promisify(setImmediate);

setImmediatePromise('foobar').then((value) => {
// value === 'foobar' (passing values is optional)
// This is executed after all I/O callbacks.
});

// Or with async function
async function timerExample() {
console.log('Before I/O callbacks');
await setImmediatePromise();
console.log('After I/O callbacks');
}
timerExample();
```
[`timersPromises.setImmediate()`][].

### `setInterval(callback[, delay[, ...args]])`
<!-- YAML
Expand All @@ -208,6 +190,9 @@ set to `1`. Non-integer delays are truncated to an integer.

If `callback` is not a function, a [`TypeError`][] will be thrown.

This method has a custom variant for promises that is available using
[`timersPromises.setInterval()`][].

### `setTimeout(callback[, delay[, ...args]])`
<!-- YAML
added: v0.0.1
Expand All @@ -232,17 +217,7 @@ will be set to `1`. Non-integer delays are truncated to an integer.
If `callback` is not a function, a [`TypeError`][] will be thrown.

This method has a custom variant for promises that is available using
[`util.promisify()`][]:

```js
const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);

setTimeoutPromise(40, 'foobar').then((value) => {
// value === 'foobar' (passing values is optional)
// This is executed after about 40 milliseconds.
});
```
[`timersPromises.setTimeout()`][].

## Cancelling timers

Expand All @@ -257,8 +232,7 @@ returned Promises will be rejected with an `'AbortError'`.
For `setImmediate()`:

```js
const util = require('util');
const setImmediatePromise = util.promisify(setImmediate);
const { setImmediate: setImmediatePromise } = require('timers/promises');

const ac = new AbortController();
const signal = ac.signal;
Expand All @@ -276,8 +250,7 @@ ac.abort();
For `setTimeout()`:

```js
const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);
const { setTimeout: setTimeoutPromise } = require('timers/promises');

const ac = new AbortController();
const signal = ac.signal;
Expand Down Expand Up @@ -478,6 +451,8 @@ const interval = 100;
[`setImmediate()`]: #timers_setimmediate_callback_args
[`setInterval()`]: #timers_setinterval_callback_delay_args
[`setTimeout()`]: #timers_settimeout_callback_delay_args
[`util.promisify()`]: util.md#util_util_promisify_original
[`timersPromises.setImmediate()`]: #timers_timerspromises_setimmediate_value_options
[`timersPromises.setInterval()`]: #timers_timerspromises_setinterval_delay_value_options
[`timersPromises.setTimeout()`]: #timers_timerspromises_settimeout_delay_value_options
[`worker_threads`]: worker_threads.md
[primitive]: #timers_timeout_symbol_toprimitive