Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
doc: improve awaitable timers docs
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed Apr 6, 2021
commit c6bdd6da895e3056c2d8fae36785c4d1915abf3e
88 changes: 79 additions & 9 deletions doc/api/timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,38 +333,90 @@ The `timers/promises` API provides an alternative set of timer functions
that return `Promise` objects. The API is accessible via
`require('timers/promises')`.

```js
const timersPromises = require('timers/promises');
```mjs
import {
setTimeout,
setImmediate,
setInterval,
} from 'timers/promises';
```

```cjs
const {
setTimeout,
setImmediate,
setInterval,
} = require('timers/promises');
```

### `timersPromises.setTimeout([delay[, value[, options]]])`
<!-- YAML
added: v15.0.0
-->

* `delay` {number} The number of milliseconds to wait before resolving the
`Promise`. **Default:** `1`.
* `value` {any} A value with which the `Promise` is resolved.
* `delay` {number} The number of milliseconds to wait before fulfilling the
promise. **Default:** `1`.
* `value` {any} A value with which the promise is fulfilled.
* `options` {Object}
* `ref` {boolean} Set to `false` to indicate that the scheduled `Timeout`
should not require the Node.js event loop to remain active.
**Default:** `true`.
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
cancel the scheduled `Timeout`.

```mjs
import {
setTimeout,
} from 'timers/promises';

const res = await setTimeout(100, 'result');

console.log(res); // Prints 'result'
```

```cjs
const {
setTimeout,
} = require('timers/promises');

setTimeout(100, 'result').then((res) => {
console.log(res); // Prints 'result'
});
```

### `timersPromises.setImmediate([value[, options]])`
<!-- YAML
added: v15.0.0
-->

* `value` {any} A value with which the `Promise` is resolved.
* `value` {any} A value with which the promise is fulfilled.
* `options` {Object}
* `ref` {boolean} Set to `false` to indicate that the scheduled `Immediate`
should not require the Node.js event loop to remain active.
**Default:** `true`.
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
cancel the scheduled `Immediate`.

```mjs
import {
setImmediate,
} from 'timers/promises';

const res = await setImmediate('result');

console.log(res); // Prints 'result'
```

```cjs
const {
setImmediate,
} = require('timers/promises');

setImmediate('result').then((res) => {
console.log(res); // Prints 'result'
});
```

### `timersPromises.setInterval([delay[, value[, options]]])`
<!-- YAML
added: v15.9.0
Expand All @@ -383,10 +435,28 @@ Returns an async iterator that generates values in an interval of `delay` ms.
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
cancel the scheduled `Timeout` between operations.

```js
```mjs
import {
setInterval,
} from 'timers/promises';

const interval = 100;
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)
break;
}
console.log(Date.now());
```

```cjs
const {
setInterval,
} = require('timers/promises');
const interval = 100;

(async function() {
const { setInterval } = require('timers/promises');
const interval = 100;
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
Expand Down