Skip to content

Commit

Permalink
fix: handle Uncaught in promise
Browse files Browse the repository at this point in the history
  • Loading branch information
徐博 committed Jul 28, 2021
1 parent 245d669 commit 0805fb5
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions src/db_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,21 @@ async function getItemFromDB (
primaryKeyValue: any
) {
if (db.objectStoreNames.contains(tableName)) {
const trans = db.transaction(tableName, 'readonly');
const trans: any = db.transaction(tableName, 'readonly');
const table = trans.objectStore(tableName);
const itemInTable = ((await table.get(
primaryKeyValue
)) as any) as ItemInTable;
return itemUnwrapper(itemInTable) as any;
try {
const itemInTable = ((await table.get(
primaryKeyValue
)) as any) as ItemInTable;
await trans.complete;

This comment has been minimized.

Copy link
@sylvia1106

sylvia1106 Jul 28, 2021

Owner

await trans.complete in finally block would be better?

return itemUnwrapper(itemInTable) as any;
} catch (error) {
try {
await trans.complete;
} catch (e) {
}
throw error;
}
} else {
return null;
}
Expand Down Expand Up @@ -384,26 +393,35 @@ export async function getItemsInRange (
} else {
const trans = db.transaction(tableName, 'readonly');
const table = trans.objectStore(tableName);
if (!indexRange) {
// Get all items in table if indexRange is undefined
let wrappedItems = await table.getAll();
items = (wrappedItems || [])
.map(itemUnwrapper)
.filter((item: any) => {
return item !== null;
try {
if (!indexRange) {
// Get all items in table if indexRange is undefined
let wrappedItems = await table.getAll();
items = (wrappedItems || [])
.map(itemUnwrapper)
.filter((item: any) => {
return item !== null;
});
} else {
await new Promise(function (resolve) {
table.index(indexRange.indexName).iterateCursor(indexRange2DBKey(indexRange), (cursor: any) => {
if (!cursor) {
resolve();
return;
}
var item = itemUnwrapper(cursor.value);
item && items.push(item);
cursor.continue();
});
});
} else {
await new Promise(function (resolve) {
table.index(indexRange.indexName).iterateCursor(indexRange2DBKey(indexRange), (cursor: any) => {
if (!cursor) {
resolve();
return;
}
var item = itemUnwrapper(cursor.value);
item && items.push(item);
cursor.continue();
});
});
}
await trans.complete;
} catch (error) {
try {
await trans.complete;
} catch (e) {
}
throw error;
}
}
return items;
Expand Down

0 comments on commit 0805fb5

Please sign in to comment.