我在Reddit上看到了sync-promise,并与作者进行了讨论.我们注意到IndexedDB事务和promise之间的关系有一些奇怪的不一致.
所有onsuccess事件完成后,IndexedDB事务会自动提交.一个复杂的问题是,onsuccess除了在同一个事务上执行另一个操作之外,您不能在回调中执行任何异步操作.例如,您无法在a中启动AJAX请求onsuccess,然后在AJAX请求返回某些数据后重用相同的事务.
承诺与它有什么关系?据我了解,承诺解析应该始终是异步的.这意味着如果不自动提交IndexedDB事务,则无法使用promises.
var openRequest = indexedDB.open("library");
openRequest.onupgradeneeded = function() {
// The database did not previously exist, so create object stores and indexes.
var db = openRequest.result;
var store = db.createObjectStore("books", {keyPath: "isbn"});
var titleIndex = store.createIndex("by_title", "title", {unique: true});
var authorIndex = store.createIndex("by_author", "author");
// Populate with initial data.
store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567});
store.put({title: "Bedrock Nights", author: …Run Code Online (Sandbox Code Playgroud)