Skip to content

Commit 15b8e6b

Browse files
committed
timers: graduate awaitable timers and improve docs
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #38112 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 944a956 commit 15b8e6b

File tree

1 file changed

+83
-11
lines changed

1 file changed

+83
-11
lines changed

doc/api/timers.md

+83-11
Original file line numberDiff line numberDiff line change
@@ -323,46 +323,100 @@ Cancels a `Timeout` object created by [`setTimeout()`][].
323323
## Timers Promises API
324324
<!-- YAML
325325
added: v15.0.0
326+
changes:
327+
- version: REPLACEME
328+
pr-url: https://github.com/nodejs/node/pull/38112
329+
description: Graduated from experimental.
326330
-->
327331

328-
> Stability: 1 - Experimental
329-
330332
The `timers/promises` API provides an alternative set of timer functions
331333
that return `Promise` objects. The API is accessible via
332334
`require('timers/promises')`.
333335

334-
```js
335-
const timersPromises = require('timers/promises');
336+
```mjs
337+
import {
338+
setTimeout,
339+
setImmediate,
340+
setInterval,
341+
} from 'timers/promises';
342+
```
343+
344+
```cjs
345+
const {
346+
setTimeout,
347+
setImmediate,
348+
setInterval,
349+
} = require('timers/promises');
336350
```
337351

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

343-
* `delay` {number} The number of milliseconds to wait before resolving the
344-
`Promise`. **Default:** `1`.
345-
* `value` {any} A value with which the `Promise` is resolved.
357+
* `delay` {number} The number of milliseconds to wait before fulfilling the
358+
promise. **Default:** `1`.
359+
* `value` {any} A value with which the promise is fulfilled.
346360
* `options` {Object}
347361
* `ref` {boolean} Set to `false` to indicate that the scheduled `Timeout`
348362
should not require the Node.js event loop to remain active.
349363
**Default:** `true`.
350364
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
351365
cancel the scheduled `Timeout`.
352366

367+
```mjs
368+
import {
369+
setTimeout,
370+
} from 'timers/promises';
371+
372+
const res = await setTimeout(100, 'result');
373+
374+
console.log(res); // Prints 'result'
375+
```
376+
377+
```cjs
378+
const {
379+
setTimeout,
380+
} = require('timers/promises');
381+
382+
setTimeout(100, 'result').then((res) => {
383+
console.log(res); // Prints 'result'
384+
});
385+
```
386+
353387
### `timersPromises.setImmediate([value[, options]])`
354388
<!-- YAML
355389
added: v15.0.0
356390
-->
357391

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

400+
```mjs
401+
import {
402+
setImmediate,
403+
} from 'timers/promises';
404+
405+
const res = await setImmediate('result');
406+
407+
console.log(res); // Prints 'result'
408+
```
409+
410+
```cjs
411+
const {
412+
setImmediate,
413+
} = require('timers/promises');
414+
415+
setImmediate('result').then((res) => {
416+
console.log(res); // Prints 'result'
417+
});
418+
```
419+
366420
### `timersPromises.setInterval([delay[, value[, options]]])`
367421
<!-- YAML
368422
added: v15.9.0
@@ -381,10 +435,28 @@ Returns an async iterator that generates values in an interval of `delay` ms.
381435
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
382436
cancel the scheduled `Timeout` between operations.
383437

384-
```js
438+
```mjs
439+
import {
440+
setInterval,
441+
} from 'timers/promises';
442+
443+
const interval = 100;
444+
for await (const startTime of setInterval(interval, Date.now())) {
445+
const now = Date.now();
446+
console.log(now);
447+
if ((now - startTime) > 1000)
448+
break;
449+
}
450+
console.log(Date.now());
451+
```
452+
453+
```cjs
454+
const {
455+
setInterval,
456+
} = require('timers/promises');
457+
const interval = 100;
458+
385459
(async function() {
386-
const { setInterval } = require('timers/promises');
387-
const interval = 100;
388460
for await (const startTime of setInterval(interval, Date.now())) {
389461
const now = Date.now();
390462
console.log(now);

0 commit comments

Comments
 (0)