Skip to content

Commit

Permalink
Better errors from shortcut methods (jakearchibald#181)
Browse files Browse the repository at this point in the history
* Errors from DB operation should take priority

* Update deps and simplify tests

* Testing the fix

* Documenting the benefits of awaiting operations

* Updating build
  • Loading branch information
jakearchibald authored May 12, 2020
1 parent 290833d commit 9d5153a
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 64 deletions.
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ An IDB transaction auto-closes if it doesn't have anything left do once microtas
const tx = db.transaction('keyval', 'readwrite');
const store = tx.objectStore('keyval');
const val = (await store.get('counter')) || 0;
store.put(val + 1, 'counter');
await store.put(val + 1, 'counter');
await tx.done;
```

Expand All @@ -162,7 +162,7 @@ const val = (await store.get('counter')) || 0;
// This is where things go wrong:
const newVal = await fetch('/increment?val=' + val);
// And this throws an error:
store.put(newVal, 'counter');
await store.put(newVal, 'counter');
await tx.done;
```

Expand Down Expand Up @@ -215,10 +215,15 @@ Transactions have a `.done` promise which resolves when the transaction complete

```js
const tx = db.transaction(storeName, 'readwrite');
tx.store.put('foo', 'bar');
await tx.done;
await Promise.all([
tx.store.put('bar', 'foo'),
tx.store.put('world', 'hello'),
tx.done,
]);
```

If you're writing to the database, `tx.done` is the signal that everything was successfully committed to the database. However, it's still beneficial to await the individual operations, as you'll see the error that caused the transition to fail.

## `IDBCursor` enhancements

Cursor advance methods (`advance`, `continue`, `continuePrimaryKey`) return a promise for the cursor, or null if there are no further values to provide.
Expand Down Expand Up @@ -338,17 +343,19 @@ async function demo() {
// Add multiple articles in one transaction:
{
const tx = db.transaction('articles', 'readwrite');
tx.store.add({
title: 'Article 2',
date: new Date('2019-01-01'),
body: '',
});
tx.store.add({
title: 'Article 3',
date: new Date('2019-01-02'),
body: '',
});
await tx.done;
await Promise.all([
tx.store.add({
title: 'Article 2',
date: new Date('2019-01-01'),
body: '',
}),
tx.store.add({
title: 'Article 3',
date: new Date('2019-01-02'),
body: '',
}),
tx.done,
]);
}

// Get all the articles in date order:
Expand Down
2 changes: 1 addition & 1 deletion build/cjs/database-extras.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function getMethod(target, prop) {
let target = tx.store;
if (useIndex)
target = target.index(args.shift());
const returnVal = target[targetFuncName](...args);
const returnVal = await target[targetFuncName](...args);
if (isWrite)
await tx.done;
return returnVal;
Expand Down
2 changes: 1 addition & 1 deletion build/esm/database-extras.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function getMethod(target, prop) {
let target = tx.store;
if (useIndex)
target = target.index(args.shift());
const returnVal = target[targetFuncName](...args);
const returnVal = await target[targetFuncName](...args);
if (isWrite)
await tx.done;
return returnVal;
Expand Down
2 changes: 1 addition & 1 deletion build/iife/index-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d5153a

Please sign in to comment.