.promise([type][, target]) (since jQuery 1.6) returns a Promise that resolves when all actions of a given type bound to the collection — queued or not — have finished. The default type is "fx", the effects queue used by .animate(), .fadeIn(), .slideUp(), and friends.
This is the collection method $(selector).promise(). It is not deferred.promise(), which returns a read-only view of a Deferred you created yourself. Attach .done() (or .then()) to run code after animations end, or pass the promise to $.when() to combine it with other async work.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$el.promise(); // default queue: "fx"
$el.promise("fx"); // same, explicit
$el.promise("tasks"); // custom named queue
$el.promise("fx", myObject); // attach promise methods to myObject
// Typical use
$el.fadeOut(400).fadeIn(400);
$el.promise().done(function () {
// runs after BOTH effects finish
});
Call
What it does
.promise()
Returns a Promise for the "fx" queue. Already resolved when nothing is animating.
.promise("name")
Observes a custom queue instead of "fx".
.promise(type, target)
Adds promise methods to target and returns target.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
type
Optional string. The queue type to observe. Default is "fx".
target
Optional object. If provided, promise methods are attached to it and it is returned instead of a new promise.
Return Value
Return Value
Description
Promise
A read-only promise with .done(), .fail(), .then(), .always(), and .state(). Resolves when matching animations on the whole collection finish.
target
When a target object is passed, that object is returned with the promise methods added.
jQuery Examples
Example 1 — Immediate Resolved Promise
When nothing is animating, .promise() returns a promise that is already resolved, so .done() runs right away.
jQuery
$("#check").on("click", function () {
var $box = $("#idle");
var p = $box.promise();
$("#status").text("state before done: " + p.state());
p.done(function () {
$("#status").append(" done() ran immediately");
});
});
Running effect…
(about 1.2 s later)
$.when(): effect finished.
How It Works
Return.effect() returns the collection’s promise.
Combine.$.when() resolves when all passed promises resolve.
Extend. Add more promises (such as $.ajax()) to wait for them together.
Frequently Asked Questions
.promise() returns a Promise that is resolved once every action of a given type bound to the matched collection — queued or not — has finished. By default it watches the "fx" effects queue, so it is the standard way to run code after fadeIn(), slideUp(), animate(), and similar effects complete.
$(selector).promise() is a collection method that observes animations on elements. deferred.promise() is a method of a Deferred object that returns a read-only view of that Deferred so outside code cannot resolve or reject it. They share the word "promise" but solve different problems.
The returned Promise is already resolved, so .done() callbacks run immediately (synchronously). This makes .promise() safe to use even when you are not sure whether an animation was queued.
type is the queue name to observe and defaults to "fx". target is an optional object; when supplied, the promise methods (done, fail, then, and so on) are attached to it and it is returned instead of a new promise object.
Yes. Return $el.promise() from a function and pass the result to $.when(). The combined promise resolves when all passed promises resolve, so you can wait for several effects — or an effect plus an Ajax request — before continuing.
The promise is tied to a Deferred stored in the element’s .data(). .remove() deletes that data along with the element, so any unresolved promise will never resolve. If you must take the element out of the DOM early, use .detach() and call .removeData() after the promise resolves.