javascripthtmlindexeddb

How can I wait until an IndexedDB instance is closed?


The IndexedDB method close is asynchronous (in the sense that close returns immediately and the connection is closed in a separate thread), how can I wait until close has finished? My usecase is that I close IndexedDB after automated tests and then before the next test, delete the database and re-open it.

The problem I'm seeing is that sometimes indexedDB.deleteDatabase fires the blocked event because the database hasn't yet been closed asynchronously (as you can see from deleteDatabase's documentation that I linked to). What I'm not entirely sure about is if the database will still be deleted in this case, despite the blocked event being fired.


Solution

  • You don't need to wait for close completed events, just close, delete the database and re-open it.

    As you can see in IndexedDB API doc, close method does not dispatch completed event, but database delete method does. Anyways you don't need to listen these events.