jQuery.Deferred.getStackHook() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

jQuery.Deferred.getStackHook is the older, deprecated name of getErrorHook. Like it, this is an optional function you define: jQuery calls it (with no arguments) when it queues an async .then() callback, then passes the return value to exceptionHook if the callback throws. jQuery only uses it when getErrorHook is not defined. It is an advanced diagnostics API.

Syntax

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

jQuery
// Older name (deprecated in 3.7.0)
jQuery.Deferred.getStackHook = function () {
  return new Error().stack;          // a stack string (an Error also works)
};

jQuery.Deferred.exceptionHook = function (error, asyncError) {
  console.log(asyncError);           // the value returned above
};

// Preferred today:
jQuery.Deferred.getErrorHook = function () { return new Error(); };
CallWhat it does
getStackHook = function () { return stack }Record a stack (string) or Error whenever a .then() callback is queued.
exceptionHook(error, asyncError)Receives the recorded value as asyncError (named stack in older docs).
getErrorHookThe modern replacement — when defined, getStackHook is ignored.
typeof jQuery.Deferred.getStackHook"undefined" in stock jQuery; only exists if you or a tool define it.

New code should define getErrorHook instead. Keep getStackHook only to support very old setups.

Parameters

ParameterDescription
(none)jQuery calls getStackHook() with no arguments. You assign a function; there is nothing to pass.

Return Value

Return ValueDescription
String or ErrorTypically new Error().stack (a string) or an Error. jQuery does not inspect it; it simply hands it to exceptionHook.
undefinedIf neither this hook nor getErrorHook is defined, exceptionHook receives undefined as asyncError.

jQuery Examples

Example 1 — Return a Stack String

The classic use: return new Error().stack and read it in exceptionHook.

jQuery
jQuery.Deferred.getStackHook = function () {
  return new Error().stack;   // a string
};

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

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

How It Works

  1. Define. getStackHook returns a stack string when a callback is queued.
  2. Throw. The .then() callback throws an error.
  3. Receive. exceptionHook gets the saved string as asyncError.

Example 2 — Ignored When getErrorHook Exists

Count the calls to see which hook jQuery actually uses.

jQuery
var errorCalls = 0, stackCalls = 0;

jQuery.Deferred.getErrorHook = function () { errorCalls++; return new Error("e"); };
jQuery.Deferred.getStackHook = function () { stackCalls++; return "s"; };

jQuery.Deferred.exceptionHook = function () {
  console.log("getErrorHook calls: " + errorCalls);   // 1
  console.log("getStackHook calls: " + stackCalls);   // 0
};

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

How It Works

  1. Both defined. Two counters track how often each hook runs.
  2. Priority. jQuery checks getErrorHook first and skips getStackHook.
  3. Result. Only the error hook was called once; the stack hook never ran.

Example 3 — Migrate to getErrorHook

Switch the name and return an Error instead of a string.

jQuery
// Before (deprecated)
// jQuery.Deferred.getStackHook = function () { return new Error().stack; };

// After (jQuery 3.7+)
jQuery.Deferred.getErrorHook = function () {
  return new Error();
};

jQuery.Deferred.exceptionHook = function (error, asyncError) {
  console.log(asyncError instanceof Error);          // true
  console.log(typeof asyncError.stack);              // "string"
};

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

How It Works

  1. Rename. Change getStackHook to getErrorHook.
  2. Return an Error. Drop .stack; the Error carries it for you.
  3. Read it. asyncError.stack still gives the string when you need it.

Frequently Asked Questions

It is an optional hook, not defined by stock jQuery, that jQuery calls when it queues an async .then() callback. Whatever you return is passed to jQuery.Deferred.exceptionHook as asyncError if the callback throws. It was the original way to capture where a Deferred callback was set up.
Yes. jQuery 3.7.0 introduced getErrorHook as the preferred name and kept getStackHook as a deprecated alias. The old name suggested a stack string, but jQuery simply passes the value along, and an Error instance works better with source maps.
getErrorHook. jQuery checks for it first and only falls back to getStackHook when getErrorHook is not defined.
Usually new Error().stack, which is a string, though returning an Error also works because jQuery does not inspect the value. In new code, define getErrorHook and return the Error itself.
Rename jQuery.Deferred.getStackHook to jQuery.Deferred.getErrorHook and return new Error() instead of new Error().stack. If you still need the text, read asyncError.stack inside exceptionHook.
No. Capturing a stack for every queued .then() callback adds overhead. These hooks are meant for development and debugging. Remove them, or guard them with a debug flag, before shipping.

Next: Utilities Methods

Explore helper functions like $.each(), $.extend(), and $.parseHTML().

Utilities Methods 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