ERR_ASYNC_CALLBACK
Node.jsERRORCriticalAsyncHIGH confidence

Async hook callback must be a function

Production Risk

Low — caught at hook registration time.

What this means

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.

Why it happens
  1. 1Passing an object or null where an async hook callback function is expected
  2. 2Typo in the callback property name in the hooks object
  3. 3Passing a non-function as the destroy callback
How to reproduce

Triggered when async_hooks.createHook() validates its callbacks object.

trigger — this will error
trigger — this will error
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

Pass function values for all async hook callbacks
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.

Code examples
Triggerjs
const async_hooks = require('async_hooks');
async_hooks.createHook({
  init: 'not a function', // must be a function
});  // this triggers ERR_ASYNC_CALLBACK
Handle in try/catchjs
try {
  // 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
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_async_callback(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Pass strings, objects, or null as async hook callbacks

Only functions are valid; the async_hooks API is strict about callback types.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors