I'm currently trying to wrap some indexedDB code into promises.
I have a "load db" function as:
db.load = new RSVP.Promise( function ( fulfill , reject ) {
//...
if (globaldb) {
fulfill(globaldb);
return;
}
//...
request.onsuccess = function(e) {
globaldb = e.target.result;
fulfill(globaldb);
}
});
My intent is to load the db the on the first DB function called, saving the reference to it and reusing it on subsequent requests.
db.query = function( objectStoreName , options ) {
var queryPromise = new RSVP.Promise( function ( fulfill , reject ) {
//... do some work
transaction.oncomplete = function() {
fulfill( anArray );
}
});
return db.load.then(queryPromise);
}
Finally, trying to use the wrapper created above:
db.query("tablename", ... ).then( function ( data ) {
//do things, write to the screen, etcetera
});
It ends up that data
contains the value fulfilled by db.load
, instead of the one fulfilled by db.query
. How can I fix that? Is there a better way to achieve the same goal?