JavaScript & jQuery Tutorials

JQ Home Right Arrow Objects Right Arrow deferred.rejectWith()

deferred.rejectWith()

failCallbacks rejection.

Description

The deferred.rejectWith() Deferred method, rejects a Deferred object and calls any failCallbacks with the specified context and arguments.

  • Only the creator of a Deferred object should call this method. Other code can be stopped from changing Deferred object state or from reporting Deferred object status by returning a restricted Promise object through the deferred.promise() method.
  • When the deferred.rejectWith() method is called, any failCallbacks added by the deferred.then() or deferred.fail() methods are called.
  • Callbacks are executed in the order they were added and each callback is passed the context from the deferred.rejectWith() method and args if any were specified.
  • Any failCallbacks added after the Deferred object enters the rejected state are executed immediately when they are added, using the arguments that were passed to the deferred.rejectWith() method.
  • When using the deferred.rejectWith() method any arguments passed have to be wrapped in an array. You can call deferred.reject() in the desired context, without the overhead of passing an array, as the method passes the context on to the callbacks fired.

Syntax


Signature Description
deferred.rejectWith( context, [args] )Reject a Deferred object and call any failCallbacks with the specified context and arguments.

Parameters


Parameter Description Type
context Context passed to the failCallbacks as the this special operator.Object
args Array of arguments that are passed to the progressCallbacks.Array

Return

A Deferred object.

deferred.rejectWith( context, [args] ) Examplesgo to Objects Objects   go to top of page Top

Reject a Deferred object and call any failCallbacks with the specified context and arguments.

In the example below when we press the left button the first time we create a Deferred object and use some deferred object methods on it. We then reject the Deferred object with one argument and process any failCallbacks.

When we press the right button the first time we create a Deferred object and use some deferred object methods on it. We then reject the Deferred object with two arguments and process any failCallbacks.


$(function(){
  $('#btn19').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.fail( iFunc );
    ourDeferred.rejectWith( this, ['#div1'] );
  });
  $('#btn20').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.fail( jFunc );
    ourDeferred.rejectWith( this, ['function was fired from our deferred.', '#div1']);
  });
  function iFunc( div ){
    $(div).append( 'In iFunc: The value of the context passed as the "this" object is : ' 
    		       + $(this).attr("id") + '. <br />');
  }
  function jFunc( value, div ){
    $(div).append( 'In jFunc: The value of the context passed as the "this" object is : ' 
    		       + $(this).attr("id") + '. <br /> Value passed: ' + value + ' <br />');
  }
});

div1. Some initial text.

Press the button below to action the above code:

               

Related Tutorials

jQuery Advanced Tutorials - Lesson 7 - The Deferred Object



 

go to jQuery home page jQ Home go to Objects Objects go to top of page Top