-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
util: add util.promisify() #12442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: add util.promisify() #12442
Changes from 1 commit
059f296
99da8e8
3ea2301
e965ed1
e7c5145
fbcb4f5
fe5ca3f
faf6654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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: #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>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add a note here that it is not yet possible to cancel a timer promise given that there is no standard way of canceling a promise.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, but I didn’t mention the reason for why that doesn’t exist – I don’t think we do that in our docs |
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const timers = require('timers'); | ||
| const { promisify } = require('util'); | ||
|
|
||
| /* eslint-disable no-restricted-syntax */ | ||
|
|
||
| common.crashOnUnhandledRejection(); | ||
|
|
||
| const setTimeout = promisify(timers.setTimeout); | ||
| const setImmediate = promisify(timers.setImmediate); | ||
|
|
||
| { | ||
| const promise = setTimeout(1); | ||
| promise.then(common.mustCall((value) => { | ||
| assert.strictEqual(value, undefined); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| const promise = setTimeout(1, 'foobar'); | ||
| promise.then(common.mustCall((value) => { | ||
| assert.strictEqual(value, 'foobar'); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| const promise = setImmediate(); | ||
| promise.then(common.mustCall((value) => { | ||
| assert.strictEqual(value, undefined); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| const promise = setImmediate('foobar'); | ||
| promise.then(common.mustCall((value) => { | ||
| assert.strictEqual(value, 'foobar'); | ||
| })); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An async function example would be cool as well.