Async hook callback must be a function
Production Risk
Low — caught at hook registration time.
Thrown when a value passed as a callback to async_hooks.createHook() (such as the init, before, after, or destroy callbacks) is not a function. All async hook lifecycle hooks must be functions if provided.
- 1Passing an object or null where an async hook callback function is expected
- 2Typo in the callback property name in the hooks object
- 3Passing a non-function as the destroy callback
Triggered when async_hooks.createHook() validates its callbacks object.
const async_hooks = require('async_hooks');
async_hooks.createHook({
init: 'not a function', // must be a function
});expected output
TypeError [ERR_ASYNC_CALLBACK]: hook callbacks must be of type function. Received type string ('not a function')Fix
Pass function values for all async hook callbacks
WHEN When registering async hooks
const async_hooks = require('async_hooks');
const hook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
// track async resource
},
destroy(asyncId) {
// cleanup
},
});
hook.enable();Why this works
All lifecycle hooks must be functions; the createHook() API validates types before registering.
const async_hooks = require('async_hooks');
async_hooks.createHook({
init: 'not a function', // must be a function
}); // this triggers ERR_ASYNC_CALLBACKtry {
// operation that may throw ERR_ASYNC_CALLBACK
riskyOperation()
} catch (err) {
if (err.code === 'ERR_ASYNC_CALLBACK') {
console.error('ERR_ASYNC_CALLBACK:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_async_callback(...args) {
// validate args here
return performOperation(...args)
}✕ Pass strings, objects, or null as async hook callbacks
Only functions are valid; the async_hooks API is strict about callback types.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev