jQuery deferred.always() Method

This deferred.always() method in jQuery is used to add handlers which are to be called when the Deferred object is resolved or when it is rejected. The arguments specified can be either a single function or an array of functions.

Syntax:

deferred.always( alwaysCallbacks [, alwaysCallbacks] )

Parameters: This method accepts two parameters as mentioned above and described below:

  • alwaysCallbacks: This parameter specifies a function or an array of functions, which is called when the Deferred is either resolved or rejected.
  • alwaysCallbacks: This parameter specifies a number of function or an array of functions, which are called when the Deferred is either resolved or rejected. It is an optional parameter.

Return Value: This method returns the Deferred object. 

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JQuery deferred.always() method
    </title>
  
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p id="GFG_UP"></p>
  
    <button onclick="Geeks();">
        click here
    </button>
  
    <script>
        var el_up =
            document.getElementById("GFG_UP");
  
        el_up.innerHTML =
            "JQuery | deferred.always() method";
  
        function Geeks() {
  
            // Use the always() method to
            // alert the user
            $.get("testingGFG.php")
                    .always(function () {
                alert("Either $.get successfully"
                    + " completed or error "
                    + "callbacks arguments");
            });
        
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
  • After clicking the button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JQuery | deferred.always() method
    </title>
  
    <script src=
    </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>
        var el_up =
            document.getElementById("GFG_UP");
        var el_down =
            document.getElementById("GFG_DOWN");
        el_up.innerHTML =
            "JQuery | deferred.always() method";
  
        function Geeks() {
  
            // Use the always() method to
            // change the text
            $.get("testingGFG.php").always(function () {
                el_down.innerHTML = "Either $.get "
                    + "successfully completed" +
                    " or error callbacks arguments";
            });
        
    </script>
</body>
  
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.