bramski / angular-indexedDB

An angularjs serviceprovider to utilize indexedDB with angular
165 stars 49 forks source link

Detect failure at the time of opening the store #83

Closed prassh123 closed 7 years ago

prassh123 commented 7 years ago

I was wondering if there is a way to detect failure to open the store. One case that i ran into was specifying an incorrect data store name as below. In that scenario, the callback was never executed.

$indexedDB.openStore('abcd', function(readStore) {
    // perform some important operations
});
// important operations were never executed because the store 'abcd' never exists, but I want to guard against this scenario. 
bramski commented 7 years ago
$indexedDB.openStore('abcd', function (readStore) {
   // do stuff
}).catch( function (failureReason) {
  // handle error
});

openStore actually just returns a promise. I don't remember exactly why I had implemented openStore as a callback instead of promise style but I'm sure there was a good reason.

bramski commented 7 years ago

Ahhh yes that's right. You actually get a transaction out of the system for the stores requested. Your function needs to execute before control can be fully given back to you. The problem really is more around how javascript stacks promises. The library needs to execute your callback and then commit the transaction. If you do a .then() on the returned promise from openStore() you are then guaranteed that your transaction has successfully committed to the underlying store.

bramski commented 7 years ago

This may perhaps be unclear when reading the project documentation. Feel free to open a PR in a way that you feel clarifies the documentation in this regard.

prassh123 commented 7 years ago

Thanks @bramski. Catching the error (as you had described above) works well.