关于HTML5 IndexedDB的异步API疑惑 [英] Doubts about HTML5 IndexedDB Async API

查看:348
本文介绍了关于HTML5 IndexedDB的异步API疑惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 HTML5索引规格我有关于它的异步请求模式有些怀疑。当在看请求API例如,在打开方法用于启动异步请求。

  VAR请求= indexedDB.open(地址簿,地址簿);
request.onsuccess =功能(EVT){...};
request.onerror =功能(EVT){...};

目前请求开始时的时间,不存在的事件处理程序尚未定义


  • 这不是一个竞争条件?

  • 打开方法成功会发生什么的JavaScript间preTER之前执行分配到的onSuccess

  • 或者是才真正开始请求一次回调都被注册?

在我看来像下面的API会更合乎逻辑的:

  db.open(地址簿,地址簿,{
    的onSuccess:功能(E){...},
    的onerror:功能(E){...}
});


解决方案

有将没有竞争条件,因为JavaScript引擎将完成执行实际范围(功能),然后引发任何回调或事件处理程序。阅读下面的<一个href=\"http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/comment-page-1/#comment-92830\">comment在Mozilla黑客。

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) { ... }
});

解决方案

There will be no race condition because JavaScript engine will finish executing actual scope (function) and then fire any callback or event handler. Read following comment on Mozilla Hacks.

这篇关于关于HTML5 IndexedDB的异步API疑惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆