Skip to content

Instantly share code, notes, and snippets.

@arackaf
Last active January 20, 2019 01:53
Show Gist options
  • Save arackaf/007cbd2c52d5ea03b52df7c063340962 to your computer and use it in GitHub Desktop.
Save arackaf/007cbd2c52d5ea03b52df7c063340962 to your computer and use it in GitHub Desktop.
function readTable(table, idxName) {
let open = indexedDB.open("books", 1);
return new Promise(resolve => {
open.onsuccess = evt => {
let db = open.result;
let tran = db.transaction(table);
let objStore = tran.objectStore(table);
let idx = objStore.index(idxName);
let tranCursor = idx.openCursor();
let result = [];
tranCursor.onsuccess = evt => {
let cursor = evt.target.result;
if (!cursor) return resolve(result);
let item = cursor.value;
result.push(item);
cursor.continue();
};
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment