deferred.rejectWith()S2C Home « Objects « 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
Deferredobject 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 thedeferred.promise()method. - When the
deferred.rejectWith()method is called, any failCallbacks added by thedeferred.then()ordeferred.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
Deferredobject enters the rejected state are executed immediately when they are added, using the arguments that were passed to thedeferred.rejectWith()method. - When using the
deferred.rejectWith()method any arguments passed have to be wrapped in an array. You can calldeferred.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] ) ExamplesTop
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.