- IndexedDB 教程
- IndexedDB - 首頁
- IndexedDB - 簡介
- IndexedDB - 安裝
- IndexedDB - 連線
- IndexedDB - 物件儲存
- IndexedDB - 建立資料
- IndexedDB - 讀取資料
- IndexedDB - 更新資料
- IndexedDB - 刪除資料
- 使用 getAll() 函式
- IndexedDB - 索引
- IndexedDB - 範圍
- IndexedDB - 事務
- IndexedDB - 錯誤處理
- IndexedDB - 搜尋
- IndexedDB - 遊標
- IndexedDB - Promise Wrapper
- IndexedDB - Ecmascript 繫結
- IndexedDB 實用資源
- IndexedDB - 快速指南
- IndexedDB - 實用資源
- IndexedDB - 討論
IndexedDB - Promise Wrapper
Promise 類似於回撥函式,它是一種技術,用於在你想要執行程式碼來完成一個非同步操作時告訴什麼,而無需停止 javascript 的執行時執行緒。
你可以使用 promise 替代向非同步函式提供一個在該函式完成後執行的回撥函式。
Jake Archibald 建立了 promise library,它使用 promise 而非事件。
它比傳統的 IndexedDB 更容易使用。它在保持 API 結構的同時,對該 API 進行了簡化。
我們這裡只展示改進之處,關於我們可以如何使用 Promise library 的詳細資訊,你可以訪問以下網站:
它有一些改進:
- IDBDatabase
- IDBTransaction
- IDBCursor
IDBDatabase
從物件儲存中獲取或設定的快捷方式
const value = await db.get(storeName, key); await db.put(storeName, value, key);
從索引中獲取的快捷方式
const value = await db.getFromIndex(storeName, indexName, key);
IDBTransaction
tx.store
如果一個事務是一個單一儲存,則 store 屬性引用該儲存,否則它就是未定義的,然後我們使用
const tx = db.transaction('any transaction');
const store = tx.store;
tx.objectStore(storeName);
tx.done
.done promise 在一個事務成功完成後解決,否則它會因一個事務錯誤而被拒絕。
const tx = db.transaction(storeName, 'readwrite');
await Promise.all([
tx.store.add('one', 'two'),
tx.store.put('three', 'four'),
tx.done,
]);
IDBCursor
遊標前進方法為:
- Advance
- Continue
- ContinuePrimaryKey
它們返回一個指向遊標的 promise,否則返回 null。
let cursor = await db.transaction(storeName).store.openCursor();
while (cursor) {
document.write(cursor.key, cursor.value);
cursor = await cursor.continue();
}
廣告