diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-04-14 22:42:47 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-05-09 16:01:44 +0300 |
commit | e7c51454b0de0a6b33c15a4d77006a17ced4eeff (patch) | |
tree | d255139db748c4940224e6c672dbae6d372655ca /doc/api/timers.md | |
parent | e965ed16c111f219116a5e8ea6847f5b98b5d0be (diff) |
timers: add promisify support
Add support for `util.promisify(setTimeout)` and
`util.promisify(setImmediate)` as a proof-of-concept implementation.
`clearTimeout()` and `clearImmediate()` do not work on those Promises.
Includes documentation and tests.
PR-URL: https://github.com/nodejs/node/pull/12442
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: William Kapke <william.kapke@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Diffstat (limited to 'doc/api/timers.md')
-rw-r--r-- | doc/api/timers.md | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/doc/api/timers.md b/doc/api/timers.md index 2b6af6f66f4..8abcdcb5cb6 100644 --- a/doc/api/timers.md +++ b/doc/api/timers.md @@ -85,6 +85,27 @@ next event loop iteration. If `callback` is not a function, a [`TypeError`][] will be thrown. +*Note*: 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(); +``` + ### setInterval(callback, delay[, ...args]) <!-- YAML added: v0.0.1 @@ -126,12 +147,28 @@ will be set to `1`. If `callback` is not a function, a [`TypeError`][] will be thrown. +*Note*: 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. +}); +``` + ## Cancelling Timers The [`setImmediate()`][], [`setInterval()`][], and [`setTimeout()`][] methods each return objects that represent the scheduled timers. These can be used to cancel the timer and prevent it from triggering. +It is not possible to cancel timers that were created using the promisified +variants of [`setImmediate()`][], [`setTimeout()`][]. + ### clearImmediate(immediate) <!-- YAML added: v0.9.1 @@ -168,4 +205,5 @@ Cancels a `Timeout` object created by [`setTimeout()`][]. [`setImmediate()`]: timers.html#timers_setimmediate_callback_args [`setInterval()`]: timers.html#timers_setinterval_callback_delay_args [`setTimeout()`]: timers.html#timers_settimeout_callback_delay_args +[`util.promisify()`]: util.html#util_util_promisify_original [the Node.js Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick |