Search code examples
sqlitewinjspromise

WinJS SQLite runAsync all before doing something else


I have a collection of INSERT statements that gets run within a SQLite async method:

SQLite3JS.openAsync(path).then(function (db) {
    $.each(sql, function (idx, item) {
        return db.runAsync(item).done(function complete(xhr) {
            var i = 0;
        });
    });
});

I want the collection to be inserted, then do something after this has completed successfully. I tried to follow through on then() promises, but they all get called before the db.runAsync() gets fired.

Is there a clean way to do this? Basically, I have a progress ring that should be removed once all this has completed, but I cannot get this to fire correctly.


Solution

  • I think you'll want to use WinJS's ability to join multiple promises together...

    SQLite3JS.openAsync(path).then(function (db) {
        var promises = [];
        $.each(sql, function (idx, item) {
            promises.push( db.runAsync(item) );
        });
    
        return WinJS.Promis.join( promises ).then(
            function success() {
                // all done!
            },
            function error() {
                // something didn't work
            },
        );
    });