deferred.resolveWith()S2C Home « Objects « deferred.resolveWith()

doneCallbacks resolution.

Description

The deferred.resolveWith() Deferred method, resolve a Deferred object and calls any doneCallbacks 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.resolveWith() method is called, any doneCallbacks added by the deferred.then() or deferred.done() methods are called.
  • Callbacks are executed in the order they were added and each callback is passed the context from the deferred.resolveWith() method and args if any were specified.
  • Any doneCallbacks 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.resolveWith() method.
  • When using the deferred.resolveWith() method any arguments passed have to be wrapped in an array. You can call deferred.resolve() 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.resolveWith( context, [args] )Resolve a Deferred object and call any doneCallbacks with the specified context and arguments.

Parameters

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

Return

A Deferred object.

deferred.resolveWith( context, [args] ) ExamplesTop

Resolve a Deferred object and call any doneCallbacks 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 resolve the Deferred object with one argument and process any doneCallbacks.

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



$(function(){
  $('#btn21').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( iFunc );
    ourDeferred.resolveWith( this, ['#div1'] );
  });
  $('#btn22').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( jFunc );
    ourDeferred.resolveWith( 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: