Open In App

jQuery deferred.resolveWith() Method

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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: The context passed to the doneCallbacks as the 'this' object.
  • args: It is an optional parameter that are passed to the doneCallbacks.

Return Value

This method method returns the deferred object.

Example: In this example, we resolve the Deferred object with two arguments and process any doneCallbacks.

HTML
<!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;">
        GeeksforGeeks
    </h1>

    <h2>jQuery deferred.resolveWith() Method</h2>

    <button onclick="myFunction();">
        click here
    </button>
    
    <p id="result"></p>

    <script>
        function Func(val, div) {
            $(div).append(val);
        }
        function myFunction() {
            let def = $.Deferred();
            def.done(Func);
            def.resolveWith(
                this, 
                ['Deferred is Resolved by resolveWith() Method', 
                '#result']
            );
        } 
    </script>
</body>

</html>

Output

jQuery-deferred-resolveWith-Method

Example: In this example, we resolve the Deferred object with only one arguments and process any doneCallbacks.

HTML
<!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;">
        GeeksforGeeks
    </h1>

    <h2>jQuery deferred.resolveWith() Method</h2>
    
    <button onclick="myFunction();">
        Click Here
    </button>
    
    <p id="result"></p>

    <script>
        function Func(div) {
            $(div).append(
                'Deferred is resolved by resolveWith() method');
        }
        function myFunction() {
            let def = $.Deferred();
            def.done(Func);
            def.resolveWith(this, ['#result']);
        } 
    </script>
</body>

</html>

Output

resolveWith-Method

Next Article

Similar Reads