CanJS - Deferreds

canjs

http://canjs.com/docs/can.Deferred.html

What is a deferred?

A deferred is a object that is resolved in some future time, such as an AJAX.

How can we create a deferred?

var dfd = new can.Deferred();

What is the difference between these always, done, fail, … methods?

  • always: Add one or more callbacks to be unconditionally called when a Deferred is resolved or rejected.
  • done: Add one or more callbacks to be called when a Deferred is resolved.
  • fail: Add a callback to be called when a Deferred is rejected.
  • then: The then method can take up to 2 arguments. The first arguments is the done callback, and the second argument is the fail callback.
  • when: Combine two deferred objects into one.

What is the purpose of the always method?

Add one or more callbacks to be unconditionally called when a Deferred is resolved or rejected.

dfd.always(alwaysCallbacks)

In the above code, alwaysCallbacks can be a function or it can be an array of functions.

What is the purpose of the done method?

Add one or more callbacks to be called when a Deferred is resolved.

dfd.done(doneCallbacks)

In the above code, doneCallbacks can be a function or it can be an array of functions.

What is the purpose of the fail method?

Add a callback to be called when a Deferred is rejected.

dfd.fail(failCallbacks)

In the above code, failCallbacks can be a function or it can be an array of functions.

What is the purpose of the reject method?

Reject a Deferred.

dfd.reject(args);

The above code rejects the Deferred object and calls the fail callbacks with the given arguments.

What is the purpose of the rejectWidth method?

Reject a Deferred in a particular context.

deferred.rejectWith(context, arguments)

In the above code, context passed to the failCallbacks as the this object, and arguments is an array of arguments that are passed to the failCallback(s).

What is the purpose of the resolve method?

Resolve a Deferred and calls the done callbacks with the given arguments.

deferred.resolve( args )
var def = can.Deferred();
def.resolve({ animals: [ 'pig', 'cow' ] })

What is the purpose of the resolveWith method?

Resolve a Deferred in a particular context, and calls the doneCallbacks with the given arguments.

deferred.resolveWith(context, arguments)
var def = can.Deferred();
def.done(function(success) {
    this.logSuccess(success); //-> "Can rocks!"
});
var context = {
    logSuccess: function(sucess) {
        console.log(sucess);
    }
};

def.resolveWith(context, ["Can rocks!"]);

What is the purpose of the then method?

Add callbacks to a Deferred.

deferred.then(doneCallback [, failCallback])
var def = can.Deferred();
def.then(function(success) {
    console.log(success);
}, function(reason) {
    console.error(reason); //-> "Oh no! So sad."
});
def.reject("Oh no! So sad.");

What is the purpose of the when method?

Combine two deferred objects into one.

// Create a Deferred that resolves when
// all passed Deferreds resolve:
var endDfd = can.when(dfd, dfd2).then(function() {
    console.log('I open at the close.');
});

This is a wrapper API for the underlying library being used. If you're using jQuery, this is a wrapper API for jQuery.when.

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

You can also use this to wait for the results of multiple deferreds:

can.when( can.ajax('api/farm/animals'), can.ajax('api/farm/beacons') ).then(function(animals, beacons){
    // perform some logic using both the animals and beacons data
});

You can also use this for regular JavaScript objects:

$.when({ animals: [ 'cat' ] }).done(function(value){
    alert(value.animals[0]); //-> alerts 'cat'
});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License