13 Avoiding the callback hell with Deferred

This chapter covers

  • What promises are and why they're important
  • The Deferred object
  • How to manage multiple asynchronous operations
  • Resolving and rejecting a promise

13.1 Introduction to promises

13.2 The Deferred and Promise objects

13.3 The Deferred methods


$.Deferred([beforeStart])
Parameters
beforeStart (Function)
Returns
The Deferred Object.


13.3.1 Resolving or rejecting a Deferred


deferred.resolve([argument, ..., argument])
Parameters
argument (Any)
Returns
The Deferred object.


deferred.resolveWith(context[, argument, ..., argument])
Parameters
context (Object)
argument (Any)
Returns
The Deferred object.


deferred.reject([argument, ..., argument])
Parameters
argument (Any)
Returns
The Deferred object.


deferred.rejectWith(context[, argument, ..., argument])
Parameters
context (Object)
argument (Any)
Returns
The Deferred object.


13.3.2 Execute functions upon resolution or rejection


deferred.done(callbacks[, callbacks, ..., callbacks])
Parameters
callbacks (Function|Array)
Returns
The Deferred object.


deferred.fail(callbacks[, callbacks, ..., callbacks])
Parameters
callbacks (Function|Array)
Returns
The Deferred object.


13.3.3 The when() method


$.when(object[, object, ..., object])
Parameters
object (Deferred|Promise|Object)
Returns
A Promise object

function success() {}
function fail() {}
$.when($.ajax('integer.php'), $.ajax('integer.php')).done(success).fail(fail);

13.3.4 Notifying about the progress of a Deferred


deferred.notify([argument, ..., argument])
Parameters
argument (Any)
Returns
The Deferred object.


deferred.notifyWith(context[, argument, ..., argument])
Parameters
context (Object)
argument (Any)
Returns
The Deferred Object.


13.3.5 Follow the progress


*** deferred.progress(callbacks[, callbacks, ..., callbacks])***
Parameters
callbacks (Function|Array)
Returns
The Deferred object.

var deferred = $.Deferred().progress(function (value) {
  $('.progress').text(Math.floor(value) + '%');
});

13.3.6 Using the Promise object


deferred.promise([target])
Parameters
target (Object)
Returns
The Promise object.

function timeout(milliseconds) {
  var deferred = $.Deferred();
  setTimeout(deferred.resolve, milliseconds);
  return deferred.promise();
}
timeout(1000).done(function() {
  alert('I waited for 1 second!');
});

13.3.7 Take it short with then()


deferred.then(resolvedCallback[. rejectedCallback[, progressCallback]])
Parameters
resolvedCallback (Function)
rejectedCallback (Function)
progressCallback (Function)
Returns
A Promise object

animate(1000).then(
 function() {
 alert('The process is completed');
 },
 null,
 function (value) {
 $('.progress').text(Math.floor(value) + '%');
 }
);

13.3.8 Always execute a handler


deferred.always(callbacks[, callbacks, ..., callbacks])
Parameters
callbacks (Function|Array)
Returns
The Deferred object.

var deferred = $.Deferred();
deferred
 .then(
 function(value) {
 console.log('success: ' + value);
 },
 function(value) {
 console.log('fail: ' + value);
 }
 )
 .always(function() {
 console.log('I am always logged');
 });
deferred.reject('An error');

13.3.9 Determine the state of a Deferred


deferred.state()
Returns
A string representing the state of the Deferred.

assert.equal(deferred.state(), 'resolved');

13.4 Promisifying all the things


promise([type][, target])
Parameters
type (String)
target (Object)
Returns
A Promise object.

$('#square1').animate({left: 500}, 1500);
 $('#square2').animate({left: 500}, 3000);
 $('.square')
 .promise()
 .done(function() {
 alert('The animations are completed');
 });

13.5 Summary


deferred.resolve()
deferred.reject()
deferred.notify()
deferred.resolveWith()
deferred.rejectWith()
deferred.notifyWith()
deferred.done()
deferred.fail()
deferred.progress()
deferred.always()
deferred.state()

你可能感兴趣的:(13 Avoiding the callback hell with Deferred)