jQuery deferred.rejectWith() Method

This deferred.rejectWith() method in jQuery is used to reject a Deferred object and call the failCallbacks along with the given context and arguments.

Syntax:

deferred.rejectWith(context[, args])

Parameters:

  • context: This parameter is the context passed to the failCallbacks as the ‘this’ object.
  • args: This parameter is an optional array of arguments which are passed to the failCallbacks.

Return Value: This method returns the deferred object.

Example 1: In this example, we reject the Deferred object with two arguments and process any failCallbacks.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p>
        JQuery | deferred.rejectWith() method
    </p>
      
    <button onclick="Geeks();">
        click here
    </button>
      
    <p id="GFG_DOWN"></p>
  
    <script>
        function Func(val, div) {
            $(div).append(val);
        }
  
        function Geeks() {
            var def = $.Deferred();
            def.fail(Func);
            def.rejectWith(this, 
['Deferred is rejected by rejectWith() method.<br/>',
                '#GFG_DOWN']);
        
    </script>
</body>
  
</html

chevron_right


Output:

Example 2: In this example, we reject the Deferred object with only one arguments and process any failCallbacks.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p>
        JQuery | deferred.rejectWith() method
    </p>
      
    <button onclick="Geeks();">
        click here
    </button>
      
    <p id="GFG_DOWN"></p>
  
    <script>
        function Func(div) {
            $(div).append(
'Deferred is rejected by rejectWith() method');
        }
  
        function Geeks() {
            var def = $.Deferred();
            def.fail(Func);
            def.rejectWith(this, ['#GFG_DOWN']);
        
    </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.