jQuery Deferred then() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

deferred.then(doneFilter [, failFilter] [, progressFilter]) adds handlers for resolve, reject, and progress. Since jQuery 1.8 it returns a new Promise that can transform status and values.

Syntax

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

jQuery
dfd.then(onDone, onFail, onProgress)

// Filter resolve value
dfd.then(function (v) { return v * 2; })

// Fail only
dfd.then(null, onFail)

// Returns a NEW Promise (chainable)
CallWhat it does
doneFilterRuns on resolve; may transform value
failFilterRuns on reject; may transform reason
progressFilterRuns on notify / notifyWith
Return valueNew Promise (not the same Deferred)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start from a Deferred or jqXHR (e.g. $.get).
Step 2Call .then(doneFilter, failFilter) — pass null to skip a slot.
Step 3Return a value or another Promise from the filter to feed the next step.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Success and Failure Handlers

Official style: two callbacks like $.get(...).then(ok, err).

jQuery
var dfd = $.Deferred();

dfd.then(
  function (msg) {
    console.log("success: " + msg);
  },
  function (err) {
    console.log("error: " + err);
  }
);

dfd.resolve("ok");
// success: ok
Try It Yourself

How It Works

  1. Two filters. First runs on resolve; second on reject.
  2. Resolve with "ok" — only the success path runs.
  3. Reject instead would call the second function.

Example 2 — Filter the Resolve Value

Return a new value from then; downstream handlers see the filtered result.

jQuery
var defer = $.Deferred();
var filtered = defer.then(function (value) {
  return value * 2;
});

filtered.done(function (value) {
  console.log("Value is (2*5=) 10: " + value);
});

defer.resolve(5);
// Value is (2*5=) 10: 10
Try It Yourself

How It Works

  1. Create a Deferred and a filtered Promise via then.
  2. Filter multiplies the resolved value by 2.
  3. Resolve(5) on the original → filtered Promise gets 10.

Example 3 — Filter a Rejection (Recover)

Pass null for success; in jQuery 3+, returning a value from the fail filter recovers the chain.

jQuery
var defer = $.Deferred();
var filtered = defer.then(null, function (value) {
  return value * 3;  // recover with a new value
});

filtered.done(function (value) {
  console.log("recovered (3*6=) 18: " + value);
});

defer.reject(6);
// recovered (3*6=) 18: 18
Try It Yourself

How It Works

  1. null doneFilter — success path is unfiltered.
  2. failFilter runs on reject and returns 6 * 3.
  3. Promises/A+ (jQuery 3+) — a returned value fulfills the next Promise.

Frequently Asked Questions

then(doneFilter, failFilter, progressFilter) attaches handlers for resolve, reject, and progress. Since jQuery 1.8 it returns a new Promise that can filter or transform values — replacing the deprecated pipe() method.
done() and fail() only run side effects; their return values are ignored. then() filters the chain: returned values (or promises) flow to the next link, and then() itself returns a new Promise.
Yes. As of jQuery 1.8, then() provides pipe-style filtering. pipe() is deprecated in jQuery 3+ — prefer then() for new code.
A plain value (passed downstream), another Deferred/Promise (the chain waits), or a rejection (throw / rejected promise) to fail the next step.
Yes. Pass null for unused slots, e.g. then(null, failFilter) to only transform rejections.
Since jQuery 3.0, Deferred promises are Promises/A+ compatible for chaining and error bubbling, with jQuery extras like progress still available on Deferreds.

Next: deferred.pipe() Method

Learn the hook jQuery calls when a then() callback throws.

deferred.pipe() 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.

10 people found this page helpful