Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Deferred. Using $.when and .progress()

I am writing a parser littered with Async Tasks. I use JQuery promises to control and order the async tasks. This is a psuedo code version of the constructor function:

   /**
     * @constructor
     */
    function Parser(json)
    {   
        return $.when(
            this.delete().then(this.insert(json)),
            this.doSomething(),
            this.doSomethingElse().then(this.update(json))
        ) 
    };

and this is what an example function looks like:

Parser.prototype.doSomething = function()
{   
    var dfd = $.Deferred();

    exampleTask(dfd.reject, dfd.resolve);

    return dfd.promise();
};

From the JQuery docs:

In the case where multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed

How can I use .progress() to notify anybody that cares about the overall progress of the Parser? e.g.

var parser = new Parser(json);
parser.progress(function(prog){console.log(prog});

Heres a fiddle with what I'm trying to do: http://jsfiddle.net/ashanova/RjULA/2/

like image 708
Jon Wells Avatar asked Aug 22 '12 14:08

Jon Wells


People also ask

What is Deferred () used for?

Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.

Can JQuery be Deferred?

The jQuery. Deferred method can be passed an optional function, which is called just before the method returns and is passed the new deferred object as both the this object and as the first argument to the function. The called function can attach callbacks using deferred. then() , for example.

How use JQuery Deferred and promise?

promise() will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists. If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point.

What is Deferred resolve in JQuery?

This deferred. resolve() method in JQuery is used to resolve a Deferred object and call any doneCallbacks with the given arguments. Syntax: deferred.resolve([args]) Parameters: args: This is optional parameters and is arguments which are passed to the doneCallbacks.


1 Answers

Use deferred.notify() to call the progressCallbacks. For example:

function doSomething() {   
    var dfd = $.Deferred();

    var count = 0;
    var intervalId = setInterval(function() {
        dfd.notify(count++);
        count > 3 && clearInterval(intervalId);
    }, 500);

    return dfd.promise();
};

var promise = doSomething();

promise.progress(function(prog) {
  console.log(prog);
});​

DEMO.

like image 90
João Silva Avatar answered Oct 13 '22 20:10

João Silva