Node.js Reference ยท util, assert and the Test Runner
Node.js util.callbackify() function
Reference for util.callbackify() in Node.js: wrap an async function so it takes an error-first callback, how falsy rejections are handled, and when to use it.
util.callbackify(fn) does the reverse of util.promisify(). It takes an async function (or any function that returns a promise) and returns a function that takes the same arguments plus a final error-first callback. When the promise settles, the callback is called with the outcome.
You need it when you must plug modern code into an API that insists on callbacks: an older library, a plugin interface, or a public function whose callback style you must keep for compatibility.
import { callbackify } from "node:util"; async function getName() { return "Ada"; } const getNameCb = callbackify(getName); getNameCb((err, name) => console.log(err, name)); // null "Ada"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
util.callbackify(original: (...args) => Promise<T>): (...args, callback: (err: Error | null, value?: T) => void) => void
| Name | Description |
|---|---|
original |
A function that returns a promise (usually an async function). |
Returns: a function that accepts the original arguments followed by a callback. The callback is called with (null, value) on fulfillment, or (reason) on rejection.
Errors / edge cases: callbackify() throws ERR_INVALID_ARG_TYPE if original is not a function. The returned function throws the same error if its last argument is not a function. If the promise rejects with a falsy value (null, undefined, 0, ""), Node wraps it in an Error with message Promise was rejected with falsy value and code ERR_FALSY_VALUE_REJECTION; the original value is on err.reason.
Usage notes
- The callback always runs asynchronously, even when the original function finishes immediately, because it is scheduled from the promise's settlement. This avoids the "sometimes sync, sometimes async" bug (Zalgo).
- Exceptions thrown by the callback itself are not caught. They surface as an uncaught exception on a later turn of the event loop (via
process.nextTick), so a bug in your callback crashes the process rather than being swallowed by the promise. - Only the first result value is passed on: a promise has a single fulfillment value.
thisis preserved from how the returned function is called.- Use sparingly. New code should return promises; callbackify is a bridge for compatibility, not a goal.
- Opposite direction:
util.promisify().
Related
util.promisify(),util.deprecate()- Lessons: The util module