JQuery deferred.resolveWith() method
This deferred.resolveWith() method in JQuery is used to resolve a Deferred object and call the doneCallbacks along with the given context and arguments.
Syntax:
deferred.resolveWith(context[, args])
Parameters:
- context: This parameter is the context passed to the doneCallbacks as the ‘this’ object.
- args: This parameter is an optional array of arguments which are passed to the doneCallbacks.
Return Value: This method method returns the deferred object.
There are two examples discussed below:
-
Example: In this example, We resolve the Deferred object with two arguments and process any doneCallbacks.
<!DOCTYPE HTML>
<
html
>
<
head
>
<
title
>
JQuery | deferred.resolveWith() method
</
title
>
<
script
src
=
"https://code.jquery.com/jquery-3.5.0.js"
>
</
script
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h1
style
=
"color:green;"
>
w3wiki
</
h1
>
<
p
id
=
"GFG_UP"
>
</
p
>
<
button
onclick
=
"Beginner();"
>
click here
</
button
>
<
p
id
=
"GFG_DOWN"
>
</
p
>
<
script
>
var el_up = document.getElementById("GFG_UP");
el_up.innerHTML = "JQuery | deferred.resolveWith() method";
function Func(val, div){
$(div).append(val);
}
function Beginner() {
var def = $.Deferred();
def.done(Func);
def.resolveWith(
this, ['Deferred is resolved by resolveWith() method.
<
br
/>', '#GFG_DOWN']);
}
</
script
>
</
body
>
</
html
>
-
Output:
-
Example: In this example, We resolve the Deferred object with only one arguments and process any doneCallbacks.
<!DOCTYPE HTML>
<
html
>
<
head
>
<
title
>
JQuery | deferred.resolveWith() method
</
title
>
<
script
src
=
"https://code.jquery.com/jquery-3.5.0.js"
>
</
script
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h1
style
=
"color:green;"
>
w3wiki
</
h1
>
<
p
id
=
"GFG_UP"
>
</
p
>
<
button
onclick
=
"Beginner();"
>
click here
</
button
>
<
p
id
=
"GFG_DOWN"
>
</
p
>
<
script
>
var el_up = document.getElementById("GFG_UP");
el_up.innerHTML =
"JQuery | deferred.resolveWith() method";
function Func(div){
$(div).append(
'Deferred is resolved by resolveWith() method');
}
function Beginner() {
var def = $.Deferred();
def.done(Func);
def.resolveWith(this, ['#GFG_DOWN']);
}
</
script
>
</
body
>
</
html
>
-
Output:
Contact Us