'Doubts about HTML5 IndexedDB Async API

While reading the HTML5 IndexedDB Specification I had some doubts about its asynchronous request model. When looking at the request api example, the open method is used to start an async request.

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

At the time this request is started, there are no event handlers defined yet.

  • Isn't this a race condition?
  • What happens when the open method succeeds before the javascript interpreter executes the assignment to onsuccess?
  • Or is the request only really started once both callbacks are registered?

In my opinion an api like the following would be much more logical:

db.open('AddressBook', 'Address Book', {
    onsuccess: function(e) { ... },
    onerror  : function(e) { ... }
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source