简体   繁体   English

有关HTML5 IndexedDB异步API的疑问

[英]Doubts about HTML5 IndexedDB Async API

While reading the HTML5 IndexedDB Specification I had some doubts about its asynchronous request model. 在阅读HTML5 IndexedDB规范时,我对其异步请求模型有一些疑问。 When looking at the request api example , the open method is used to start an async request. 在查看请求api示例时open方法用于启动异步请求。

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 ? 如果在javascript解释器执行对onsuccess的赋值之前open方法成功,会发生什么?
  • 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: 在我看来,如下所示的api更加合乎逻辑:

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

There will be no race condition because JavaScript engine will finish executing actual scope (function) and then fire any callback or event handler. 因为JavaScript引擎将完成执行实际范围(函数),然后触发任何回调或事件处理程序,所以不会出现竞争情况。 Read following comment on Mozilla Hacks. 阅读有关Mozilla Hacks的以下评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM