{"record":{"id":"f24588e974a2d21f","repo":"denoland/deno","slug":"expected-a-function","errorCode":null,"errorMessage":"expected a function","messagePattern":"expected a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/01_core.js","lineNumber":583,"sourceCode":"  // V8 skips experimental globals while snapshotting, so the native function is\n  // only installed at runtime. In a from-scratch runtime it's already on the\n  // global when this module runs (before our wrapper replaces it below), so we\n  // can grab it directly. When restoring from a snapshot it isn't there yet, so\n  // fall back to a lazy capture on first use: our wrapper shadows the native one\n  // as an own property of the global, so momentarily remove our property to\n  // reveal the native one underneath, then restore our wrapper.\n  let nativeQueueMicrotask = window.queueMicrotask;\n  function captureNativeQueueMicrotask() {\n    const wrapper = window.queueMicrotask;\n    delete window.queueMicrotask;\n    nativeQueueMicrotask = window.queueMicrotask;\n    window.queueMicrotask = wrapper;\n    return nativeQueueMicrotask;\n  }\n\n  function queueMicrotask(cb) {\n    if (typeof cb != \"function\") {\n      throw new TypeError(\"expected a function\");\n    }\n    const enqueue = nativeQueueMicrotask ?? captureNativeQueueMicrotask();\n    return enqueue(() => {\n      try {\n        cb();\n      } catch (error) {\n        reportExceptionCallback(error);\n      }\n    });\n  }\n\n  // Some \"extensions\" rely on \"BadResource\", \"Interrupted\", \"NotCapable\"\n  // errors in the JS code (eg. \"deno_net\") so they are provided in \"Deno.core\"\n  // but later reexported on \"Deno.errors\"\n  class BadResource extends Error {\n    constructor(msg, options) {\n      super(msg, options);\n      this.name = \"BadResource\";","sourceCodeStart":565,"sourceCodeEnd":601,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/libs/core/01_core.js#L565-L601","documentation":"Deno's core bootstrap re-wraps queueMicrotask so that exceptions raised inside the microtask are routed to the runtime's exception reporting instead of disappearing. Before enqueueing anything, the wrapper enforces the platform contract: the callback must be a function, otherwise it throws TypeError('expected a function'). This lives in libs/core/01_core.js and covers every queueMicrotask call routed through the core runtime.","triggerScenarios":"Calling queueMicrotask with a non-function: `queueMicrotask(fn())` executes fn immediately and passes its (often undefined) return value; passing an optional callback that was never supplied; passing an object holding a callable field instead of the function itself.","commonSituations":"Immediately-invoked setup calls that return undefined; APIs shaped as `queueMicrotask(maybeCb)` where maybeCb is undefined; passing results of array operations that produced no function.","solutions":["Pass a function reference or wrap the call: `queueMicrotask(() => fn())`","Default optional callbacks: `queueMicrotask(cb ?? (() => {}))`, or skip the call when cb is missing","Guard with `typeof cb === 'function'` before enqueueing"],"exampleFix":"// before\nqueueMicrotask(flushQueue()); // passes flushQueue()'s undefined return value\n\n// after\nqueueMicrotask(() => flushQueue());","handlingStrategy":"type-guard","validationCode":"if (typeof cb !== 'function') {\n  throw new TypeError('expected a function');\n}\nqueueMicrotask(cb);","typeGuard":"const isFunction = (v) => typeof v === 'function';\n// usage: isFunction(cb) ? queueMicrotask(cb) : fallback();","tryCatchPattern":null,"preventionTips":["Never invoke the callback while passing it — pass the reference or wrap it in an arrow","Default optional callbacks: cb ?? (() => {}), or skip the call when absent","Type the parameter as () => void so the compiler flags non-function arguments"],"tags":["queue-microtask","callback","typeerror","deno-core"],"backgroundTag":"callback-not-a-function","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}