Book Image

Mastering JavaScript Promises

Book Image

Mastering JavaScript Promises

Overview of this book

Table of Contents (16 chapters)
Mastering JavaScript Promises
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Your own $.Deferred process


You can customize the deferred object as per your need. This is simple and can be achieved by calling out the jQuery.Deferred() method. We can also define our own process and the sequence of the flow and arrange the output as required. We can use setInterval() to set up the delays and setTimeout() to decide when to end the sequence. The scope of variable declaration decides whether the deferred object has to be locally or globally processed. If the deferred object is assigned to a local variable, we can call deferred object's resolve(), promise(), and notify() events.

Let's have a look at this example:

var myCustomPromise = process();
myCustomPromise.done(function() {
    $('#result').html('done.');
});
myCustomPromise.progress(function() {
    $('#result').html($('#result').html() + '.');
});

function process() {
    var deferred = $.Deferred();

    MyTimerCall = setInterval(function() {
        deferred.notify();
    }, 1000);

    setTimeout(function() {
 ...