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

progressCallbacks notification.

Description

The deferred.notifyWith() Deferred method, calls any progressCallbacks on a Deferred object 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.notifyWith() method is called, any progressCallbacks added by the deferred.then() or deferred.progress() methods are called.
  • Callbacks are executed in the order they were added and each callback is passed the context from the deferred.notifyWith() method and args if any were specified.
  • Any calls to the deferred.notifyWith() method after a Deferred object is resolved or rejected, as well as any progressCallbacks added after this change of state, are ignored.
  • When using the deferred.notifyWith() method any arguments passed have to be wrapped in an array. You can call deferred.notify() 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.notifyWith( context, [args] )Call any progressCallbacks on a Deferred object with the specified context and arguments.

Parameters

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

Return

A Deferred object.

deferred.notifyWith( context, [args] ) ExampleTop

Call any progressCallbacks on a Deferred object 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 notify the Deferred object with one arguments and process any progressCallbacks before resolving it.

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



$(function(){
  $('#btn17').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.done( eFunc );
    ourDeferred.progress( iFunc );
    ourDeferred.notifyWith( this, ['#div1'] );
    ourDeferred.resolve( 'Our deferred was resolved. <br><br>', '#div1' );
  });
  $('#btn18').one('click', function(){
    var ourDeferred = $.Deferred();
    ourDeferred.fail( eFunc );
    ourDeferred.progress( jFunc );
    ourDeferred.notifyWith( this, ['function was fired from our deferred.', '#div1']);
    ourDeferred.reject( 'Our deferred was rejected. <br><br>', '#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: