Skip to content

Commit

Permalink
doc: replace outdated util.promisify timer examples with references
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxyz committed Jul 5, 2021
1 parent c2e6822 commit 469e0c7
Showing 1 changed file with 10 additions and 34 deletions.
44 changes: 10 additions & 34 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 an async iterator variant 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,9 @@ const interval = 100;
[`setImmediate()`]: #timers_setimmediate_callback_args
[`setInterval()`]: #timers_setinterval_callback_delay_args
[`setTimeout()`]: #timers_settimeout_callback_delay_args
[`timersPromises.setImmediate()`]: timers.md#timerspromisessetimmediatevalue-options
[`timersPromises.setInterval()`]: timers.md#timerspromisessetintervaldelay-value-options
[`timersPromises.setTimeout()`]: timers.md#timerspromisessettimeoutdelay-value-options
[`util.promisify()`]: util.md#util_util_promisify_original
[`worker_threads`]: worker_threads.md
[primitive]: #timers_timeout_symbol_toprimitive

0 comments on commit 469e0c7

Please sign in to comment.