W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2010

onsuccess callback in race condition?

From: <victor.hong@nokia.com>
Date: Thu, 15 Jul 2010 17:13:00 +0200
To: <public-webapps@w3.org>
Message-ID: <C8649DBC.D7C3%victor.hong@nokia.com>

I would like to get some clarification on async implementation of the
indexedDB API.

Example 1:
Below is a sample code from the following blog article.

var request = window.indexedDB.open("CandyDB",
                                    "My candy store database");
request.onsuccess = function(event) {
  var db = event.result;
  if (db.version != "1") {
    // User's first visit, initialize database.
    var createdObjectStoreCount = 0;
    var objectStores = [
      { name: "kids", keyPath: "id", autoIncrement: true },
      { name: "candy", keyPath: "id", autoIncrement: true },
      { name: "candySales", keyPath: "", autoIncrement: true }
    function objectStoreCreated(event) {
      if (++createdObjectStoreCount == objectStores.length) {
        db.setVersion("1").onsuccess = function(event) {
    for (var index = 0; index < objectStores.length; index++) {
      var params = objectStores[index];
      request = db.createObjectStore(params.name, params.keyPath,
      request.onsuccess = objectStoreCreated;
  else {
    // User has been here before, no initialization required.

In this code, request is returned from request = window.indexedDB.open()
call, then request.onsuccess = function() {...} is assigned.

Maybe I don't fully understand how this IndexedDB works,
it seems that there could be a race condition between the open() operation
and request.onsuccess assignment. The open() operation could be completed
before request.onsuccess got assigned to a callback function. Does this mean
that the request.onsuccess callback function will not be called?

Is there a mechanism to assure that the callback gets called regardless
whether the open() operation completed or not when request.onsuccess is
assigned to a callback function?

Of course, the same issue exists for all other async calls.

Whether this can be assured or not, maybe the spec document can point it out
if not already in the coming draft.

Example 2:
On the other hand, in the July 15, 2010 editor's draft, 3.2.2 The IDBRequest
Interface section, there is an example:

indexedDB.request.onsuccess = function(evt) {...};
indexedDB.request.onerror = function(evt) {...};
indexedDB.open('AddressBook', 'Address Book');

This indicates that request object is a member of indexedDB interface, and
request.onsuccess can be assigned to a callback before indexDB.open() is
called, therefore is no race condition issue here.

But IndexDB is a 
interface IDBFactory {
    readonly attribute DOMStringList databases;
    IDBRequest open (in DOMString name, in DOMString description) raises

It does not appear to have a request object as member.

Can someone clarify these above 2 examples for me?

Does request.onsuccess need to be assigned to a callback before
indexDB.open() get called to avoid race condition? Like in example 2)

Maybe both examples are correct. Then what makes sure that in example 1) the
callback function get called even if open() has been completed before the
callback function is assigned to request.onsuccess?

Thank you.
Received on Thursday, 15 July 2010 16:03:30 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 18:13:10 UTC