JQuery .Deferred() method
Last Updated :
31 Jul, 2024
This JQuery.Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.
Syntax:
jQuery.Deferred([beforeStart])
- Parameters:
- beforeStart: This is a function, which is called just before the constructor returns.
Return Value: This method creates and returns a new deferred object.
There are two examples discussed below:
Example: In this example, the Deferred() is used to create a new object and after that then() method is called with notify and resolve method.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
JQuery.Deferred() method
</title>
<script src="https://code.jquery.com/jquery-3.5.0.js">
</script>
</head>
<body style="text-align:center;">
<h1 style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP">
</p>
<button onclick="Geeks();">
click here
</button>
<p id="GFG_DOWN">
</p>
<script>
let el_up = document.getElementById("GFG_UP");
el_up.innerHTML = "JQuery.Deferred() method";
function Func1(val, div) {
$(div).append("From doneCallbacks - " + val);
}
function Func2(val, div) {
$(div).append("From failCallbacks - " + val);
}
function Func3(val, div) {
$(div).append("From progressCallbacks - " + val);
}
function Geeks() {
let def = $.Deferred();
def.then(Func1, Func2, Func3);
def.notify(
'Deferred "def" is notified.<br/>', '#GFG_DOWN');
def.resolve(
'Deferred "def" is resolved.<br/>', '#GFG_DOWN');
}
</script>
</body>
</html>
Output:
Example: In this example, the Deferred() method is used and the state of Deferred object is checked.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
JQuery.Deferred() method
</title>
<script src="https://code.jquery.com/jquery-3.5.0.js">
</script>
</head>
<body style="text-align:center;">
<h1 style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP">
</p>
<button onclick="Geeks();">
click here
</button>
<p id="GFG_DOWN">
</p>
<script>
let el_up = document.getElementById("GFG_UP");
el_up.innerHTML = "JQuery.Deferred() method";
let def = $.Deferred();
def.resolve();
function Geeks() {
$('#GFG_DOWN').text(
'deferred state is ' + def.state());
}
</script>
</body>
</html>
Output:
Similar Reads
jQuery deferred.done() Method This deferred.done() method in jQuery is used to add handlers which are to be called when the deferred object is resolved.Syntax:Â deferred.done(Callbacks [, Callbacks])Parameters:Callbacks: This parameter specifies a function, or array of functions, which are called when the Deferred is resolved.Cal
1 min read
JQuery deferred.then() method This deferred.then() method in JQuery is used to add handlers which are to be called when the Deferred object is resolved, rejected, or in progress. Syntax: deferred.then(doneCallbacks[, failCallbacks][, progressCallbacks])Parameters: doneCallbacks: This is a function, or an array of functions, whic
3 min read
jQuery deferred.fail() Method The deferred.fail() method in jQuery is used to add handlers which are to be called when the Deferred object is rejected. This method accepts one or more than one arguments, which can be either a function or an array of functions. Callbacks are executed in the same order they were added. When the De
2 min read
JQuery deferred.pipe() Method The deferred.pipe() method in jQuery is used to add utility method to filter, chain Deferreds.Syntax:deferred.pipe([doneFilter][, failFilter][, progressFilter])Parameters: This method accepts three parameter as mentioned above and described below:doneFilter: It is an optional function which is calle
2 min read
JQuery deferred.state() method This deferred.state() method in JQuery is used to determine the current state of a Deferred object. Syntax: deferred.state()Return Value: This method returns the state of deferred object. There are two examples discussed below: Example: In this example, the state of deferred object 'def' is pending.
2 min read