score:1

Accepted answer

So ended up solving it.

  1. I had to actually add a console.log() for the "All Complete" output.... yeah...

  2. Promises were not being added to the promise array before the $.when() was run. I moved it into a .then() chained to the .getJSON().

  3. It seemed the group() function was resolving before it actually was. I had to set the deferred object to the changed one (d = d.then()), as well as encase the inside of the .then() in a function.

    indexedDB.deleteDatabase("_pouch_test");
    db = new PouchDB('test');
    
    function item(limit){
        var promises = [];
        var url = ""; //url to respond with up to limit responces
        var j = $.getJSON(url)
            .fail(function( jqxhr, textStatus, error ) {
                var err = textStatus + ", " + error;
                console.log( "Request Failed: " + err );
            })
            .then(function(data){
                var promises=[]
                $.each(data.items,function(i, item){
                        promises.push( db.post({item:"test"}).then(console.log("Item Finished ")) );
                });
                return $.when.apply($,promises)
            }).then(function(){
                console.log("Group Complete")
            })
            return j;
    }
    
    function group(times){
        var d = $.Deferred().resolve()
        for( var i=0; i<times; i++){
           d = d.then(function(){return item(10)} )
        }
        return d.promise()
    }
    
    function init(){
        group(3).done(function(){console.log("All Complete")})
    }
    

This outputs the following, which is as intended. Yay!

(10)Item Finished
Group Finished
(10)Item Finished
Group Finished
(10)Item Finished
Group Finished
All Complete

Related Query

More Query from same tag