can.when

  • function
 

Call a callback when a Deferred resolves.

can.when(deferred)

Parameters

  1. deferred {Deferred | Object}

    The Deferred, AJAX, or normal Objects to call the callback on.

Returns

{Deferred}

deferred if deferred is a Deferred, otherwise a Deferred that resolves to deferred.

can.when(deferred) provides the ability to execute callback function(s) typically based on a Deferred or AJAX object.

can.when( can.ajax('api/farm/animals') ).then(function(animals){
    alert(animals); //-> alerts the ajax response
});

You can also use this for regular JavaScript objects.

$.when( { animals: [ 'cat' ] } ).done(function(animals){
    alert(animals[0]); //-> alerts 'cat'
});