jQuery.Deferred.getErrorHook() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

jQuery.Deferred.getErrorHook is an optional function you define. When jQuery queues a .then() callback to run asynchronously, it calls this hook (with no arguments) and keeps whatever it returns — ideally a fresh Error, which carries a stack trace of the code that set the callback up. If that callback later throws, the saved error is passed as the second argument of exceptionHook. Added in jQuery 3.7.0 as the replacement for getStackHook. Stock jQuery does not define it.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
jQuery.Deferred.getErrorHook = function () {
  return new Error("Deferred.then() queued here");
};

jQuery.Deferred.exceptionHook = function (error, asyncError) {
  console.log(asyncError.stack);   // stack captured by getErrorHook
};

// In stock jQuery:
typeof jQuery.Deferred.getErrorHook   → "undefined"
CallWhat it does
getErrorHook = function () { return new Error() }Install the hook so every queued .then() callback records an Error.
exceptionHook(error, asyncError)Receives the saved Error as asyncError when the callback throws.
typeof jQuery.Deferred.getErrorHook"undefined" until you (or a debug tool) define it.
getErrorHook = undefinedRemove the hook to stop recording stacks.

This hook is for debugging: it creates an Error for every queued .then() callback, which costs time and memory.

Parameters

ParameterDescription
(none)jQuery calls getErrorHook() with no arguments. You do not pass anything; you assign a function to the property.

Return Value

Return ValueDescription
Error (recommended)An Error instance created where the callback was queued. Its .stack helps source maps and dev tools show the real origin.
Any valuejQuery stores whatever you return and hands it to exceptionHook as asyncError. If the hook is not defined, asyncError is undefined.

jQuery Examples

Example 1 — Receive asyncError in the Hook

Define getErrorHook and read its Error inside exceptionHook.

jQuery
jQuery.Deferred.getErrorHook = function () {
  return new Error("queued");
};

jQuery.Deferred.exceptionHook = function (error, asyncError) {
  console.log(error.message);                 // "boom"
  console.log(asyncError instanceof Error);   // true
  console.log(asyncError.message);            // "queued"
};

$.Deferred().resolve().then(function () {
  throw new Error("boom");
});
Try It Yourself

How It Works

  1. Define. getErrorHook returns a new Error each time a callback is queued.
  2. Throw. The .then() callback throws Error("boom").
  3. Read. exceptionHook gets the thrown error plus the saved asyncError.

Example 2 — Without the Hook, asyncError Is undefined

Stock jQuery does not define getErrorHook, so there is nothing to pass along.

jQuery
console.log(typeof jQuery.Deferred.getErrorHook);   // "undefined"

jQuery.Deferred.exceptionHook = function (error, asyncError) {
  console.log(typeof asyncError);                   // "undefined"
};

$.Deferred().resolve().then(function () {
  throw new Error("boom");
});
Try It Yourself

How It Works

  1. Check. typeof jQuery.Deferred.getErrorHook is "undefined" by default.
  2. Throw. The callback still throws and reaches exceptionHook.
  3. Missing. No hook was defined, so asyncError is undefined.

Example 3 — Enable It Only While Debugging

Install the hook behind a flag so production pages skip the extra cost.

jQuery
var DEBUG = true;   // set to false in production builds

if (DEBUG) {
  jQuery.Deferred.getErrorHook = function () {
    return new Error("then() queued here");
  };
}

console.log("getErrorHook installed: " +
  (typeof jQuery.Deferred.getErrorHook === "function"));   // true
Try It Yourself

How It Works

  1. Flag. A simple DEBUG constant controls the hook.
  2. Install. Only when debugging does jQuery start recording stacks.
  3. Verify. Check the property with typeof to confirm it is active.

Frequently Asked Questions

It is an optional hook, not defined by stock jQuery. If you define it, jQuery calls it (with no arguments) when it queues a .then() callback to run asynchronously, and keeps the return value. When that callback throws, the saved value is passed to jQuery.Deferred.exceptionHook as its second argument.
Because .then() callbacks run asynchronously, a thrown error's own stack no longer shows the code that set up the chain. getErrorHook captures an Error at queue time, so asyncError.stack points back to where .then() was called, which makes async bugs far easier to trace.
A new Error instance, for example return new Error("Deferred queued"). The Error's stack property records the call site and works well with browser source maps. jQuery passes whatever you return straight through, so a string also works but an Error is recommended.
getStackHook is the older name and is deprecated. The name suggested a stack string, but jQuery just passes the value along, and an Error instance cooperates better with source maps. getErrorHook was added in jQuery 3.7.0, and when both exist jQuery uses getErrorHook.
Yes. Creating an Error for each queued .then() callback captures a stack trace, which is relatively expensive. Enable it only in development or while debugging, and remove it for production.
Stock jQuery only calls it if it exists; it never defines it. Debug helpers, such as jQuery Migrate in its development build, can install it to improve error messages. You can also define it yourself.

Next: jQuery.Deferred.getStackHook() Method

See the older, deprecated alias and how to migrate to getErrorHook().

jQuery.Deferred.getStackHook() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful