@@ -323,46 +323,100 @@ Cancels a `Timeout` object created by [`setTimeout()`][].
323
323
## Timers Promises API
324
324
<!-- YAML
325
325
added: v15.0.0
326
+ changes:
327
+ - version: REPLACEME
328
+ pr-url: https://github.com/nodejs/node/pull/38112
329
+ description: Graduated from experimental.
326
330
-->
327
331
328
- > Stability: 1 - Experimental
329
-
330
332
The ` timers/promises ` API provides an alternative set of timer functions
331
333
that return ` Promise ` objects. The API is accessible via
332
334
` require('timers/promises') ` .
333
335
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' );
336
350
```
337
351
338
352
### ` timersPromises.setTimeout([delay[, value[, options]]]) `
339
353
<!-- YAML
340
354
added: v15.0.0
341
355
-->
342
356
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 .
346
360
* ` options ` {Object}
347
361
* ` ref ` {boolean} Set to ` false ` to indicate that the scheduled ` Timeout `
348
362
should not require the Node.js event loop to remain active.
349
363
** Default:** ` true ` .
350
364
* ` signal ` {AbortSignal} An optional ` AbortSignal ` that can be used to
351
365
cancel the scheduled ` Timeout ` .
352
366
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
+
353
387
### ` timersPromises.setImmediate([value[, options]]) `
354
388
<!-- YAML
355
389
added: v15.0.0
356
390
-->
357
391
358
- * ` value ` {any} A value with which the ` Promise ` is resolved .
392
+ * ` value ` {any} A value with which the promise is fulfilled .
359
393
* ` options ` {Object}
360
394
* ` ref ` {boolean} Set to ` false ` to indicate that the scheduled ` Immediate `
361
395
should not require the Node.js event loop to remain active.
362
396
** Default:** ` true ` .
363
397
* ` signal ` {AbortSignal} An optional ` AbortSignal ` that can be used to
364
398
cancel the scheduled ` Immediate ` .
365
399
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
+
366
420
### ` timersPromises.setInterval([delay[, value[, options]]]) `
367
421
<!-- YAML
368
422
added: v15.9.0
@@ -381,10 +435,28 @@ Returns an async iterator that generates values in an interval of `delay` ms.
381
435
* ` signal ` {AbortSignal} An optional ` AbortSignal ` that can be used to
382
436
cancel the scheduled ` Timeout ` between operations.
383
437
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
+
385
459
(async function () {
386
- const { setInterval } = require (' timers/promises' );
387
- const interval = 100 ;
388
460
for await (const startTime of setInterval (interval, Date .now ())) {
389
461
const now = Date .now ();
390
462
console .log (now);
0 commit comments