spacer
  • jQuery
  • UI
  • Mobile
  • Meetups
  • Forum
  • Blog
  • About
  • Donate
  • Download
  • Documentation
  • Tutorials
  • Bug Tracker
  • Discussion

jQuery API

Skip to content

deferred.then()

Categories: Deferred Object

deferred.then( doneCallbacks, failCallbacks ) Returns: Promise

Description: Add handlers to be called when the Deferred object is resolved or rejected.

  • version added: 1.5deferred.then( doneCallbacks, failCallbacks )

    doneCallbacks A function, or array of functions, called when the Deferred is resolved.

    failCallbacks A function, or array of functions, called when the Deferred is rejected.

  • version added: 1.7deferred.then( doneCallbacks, failCallbacks [, progressCallbacks] )

    doneCallbacks A function, or array of functions, called when the Deferred is resolved.

    failCallbacks A function, or array of functions, called when the Deferred is rejected.

    progressCallbacks A function, or array of functions, called when the Deferred notifies progress.

All three arguments (including progressCallbacks, as of jQuery 1.7) can be either a single function or an array of functions. The arguments can also be null if no callback of that type is desired. Alternatively, use .done(), .fail() or .progress() to set only one type of callback.

When the Deferred is resolved, the doneCallbacks are called. If the Deferred is instead rejected, the failCallbacks are called. As of jQuery 1.7, the deferred.notify() or deferred.notifyWith() methods can be called to invoke the progressCallbacks as many times as desired before the Deferred is resolved or rejected.

Callbacks are executed in the order they were added. Since deferred.then returns a Promise, other methods of the Promise object can be chained to this one, including additional .then() methods.

Example:

Since the jQuery.get method returns a jqXHR object, which is derived from a Deferred object, we can attach handlers using the .then method.


$.get("test.php").then(
    function(){ alert("$.get succeeded"); },
    function(){ alert("$.get failed!"); }
);

Support and Contributions

Need help with deferred.then() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to deferred.then()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

jQuery API

    • New or Changed in 1.7
    • Raw XML API Dump
    • Dynamic API Browser
    • jQuery API Book

Browse the jQuery API

    • All
    • Ajax
      • Global Ajax Event Handlers
      • Helper Functions
      • Low-Level Interface
      • Shorthand Methods
    • Attributes
    • Callbacks Object
    • Core
    • CSS
    • Data
    • Deferred Object
    • Deprecated
    • Dimensions
    • Effects
      • Basics
      • Custom
      • Fading
      • Sliding
    • Events
      • Browser Events
      • Document Loading
      • Event Handler Attachment
      • Event Object
      • Form Events
      • Keyboard Events
      • Mouse Events
    • Forms
    • Internals
    • Manipulation
      • Class Attribute
      • Copying
      • DOM Insertion, Around
      • DOM Insertion, Inside
      • DOM Insertion, Outside
      • DOM Removal
      • DOM Replacement
      • General Attributes
      • Style Properties
    • Miscellaneous
      • Collection Manipulation
      • Data Storage
      • DOM Element Methods
      • Setup Methods
    • Offset
    • Plugins
      • Data Link
      • Templates
    • Properties
      • Properties of jQuery Object Instances
      • Properties of the Global jQuery Object
    • Selectors
      • Attribute
      • Basic
      • Basic Filter
      • Child Filter
      • Content Filter
      • Form
      • Hierarchy
      • jQuery Extensions
      • Visibility Filter
    • Traversing
      • Filtering
      • Miscellaneous Traversing
      • Tree Traversal
    • Utilities
    • Version
      • Version 1.0
      • Version 1.0.4
      • Version 1.1
      • Version 1.1.2
      • Version 1.1.3
      • Version 1.1.4
      • Version 1.2
      • Version 1.2.3
      • Version 1.2.6
      • Version 1.3
      • Version 1.4
      • Version 1.4.1
      • Version 1.4.2
      • Version 1.4.3
      • Version 1.4.4
      • Version 1.5
      • Version 1.5.1
      • Version 1.6
      • Version 1.7
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.