diff options
author | Refael Ackermann <refack@gmail.com> | 2017-04-27 21:57:12 -0400 |
---|---|---|
committer | Refael Ackermann <refack@gmail.com> | 2017-06-10 22:49:28 -0400 |
commit | af3aa682ac534bb55765f5fef2755a88e5ff2580 (patch) | |
tree | d632fb6f8347f58c05731d16374b05706ef828ba /doc | |
parent | 780acc2208a3cdd3b01ae93aeaa478771fd3fd56 (diff) | |
download | android-node-v8-af3aa682ac534bb55765f5fef2755a88e5ff2580.tar.gz android-node-v8-af3aa682ac534bb55765f5fef2755a88e5ff2580.tar.bz2 android-node-v8-af3aa682ac534bb55765f5fef2755a88e5ff2580.zip |
util: add callbackify
Add `util.callbackify(function)` for creating callback style functions
from functions returning a `Thenable`
PR-URL: https://github.com/nodejs/node/pull/12712
Fixes: https://github.com/nodejs/CTC/issues/109
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/util.md | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/doc/api/util.md b/doc/api/util.md index 839bdadf76..9fce6294e1 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -10,6 +10,64 @@ module developers as well. It can be accessed using: const util = require('util'); ``` +## util.callbackify(original) +<!-- YAML +added: REPLACEME +--> + +* `original` {Function} An `async` function +* Returns: {Function} a callback style function + +Takes an `async` function (or a function that returns a Promise) and returns a +function following the Node.js error first callback style. In the callback, the +first argument will be the rejection reason (or `null` if the Promise resolved), +and the second argument will be the resolved value. + +For example: + +```js +const util = require('util'); + +async function fn() { + return await Promise.resolve('hello world'); +} +const callbackFunction = util.callbackify(fn); + +callbackFunction((err, ret) => { + if (err) throw err; + console.log(ret); +}); +``` + +Will print: + +```txt +hello world +``` + +*Note*: + +* The callback is executed asynchronously, and will have a limited stack trace. +If the callback throws, the process will emit an [`'uncaughtException'`][] +event, and if not handled will exit. + +* Since `null` has a special meaning as the first argument to a callback, if a +wrapped function rejects a `Promise` with a falsy value as a reason, the value +is wrapped in an `Error` with the original value stored in a field named +`reason`. + ```js + function fn() { + return Promise.reject(null); + } + const callbackFunction = util.callbackify(fn); + + callbackFunction((err, ret) => { + // When the Promise was rejected with `null` it is wrapped with an Error and + // the original value is stored in `reason`. + err && err.hasOwnProperty('reason') && err.reason === null; // true + }); + ``` + ## util.debuglog(section) <!-- YAML added: v0.11.3 @@ -955,6 +1013,7 @@ Deprecated predecessor of `console.log`. [`Object.assign()`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign [`console.error()`]: console.html#console_console_error_data_args [`console.log()`]: console.html#console_console_log_data_args +[`'uncaughtException'`]: process.html#process_event_uncaughtexception [`util.inspect()`]: #util_util_inspect_object_options [`util.promisify()`]: #util_util_promisify_original [Custom inspection functions on Objects]: #util_custom_inspection_functions_on_objects |