jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.22k stars 348 forks source link

[Question] Is it necessary to await `tx.done`? #250

Closed AlttiRi closed 2 years ago

AlttiRi commented 2 years ago

Is it really necessary to await tx.done?

It looks that it's enough:

either

await store.add(1);
await store.add(2);

or

await Promise.all([store.add(1), store.add(2)]);

In fact, it looks that I don't need even do anything above (if the both transactions are not depend each other): multiple transactions within one task execute sequentially. For example, the follow code unexpectedly works:

const store1 = db.transaction(["numbers"], "readwrite").objectStore("numbers");

store1.add(1);
store1.add(2);

const store2 = db.transaction(["numbers"], "readwrite").objectStore("numbers");

store2.add(3);
store2.add(4);

store1.add(5);
store1.add(6);

There is no exception on store1.add(5);. It performs the first transaction — adds 1, 2, 5, 6, then the second transaction — adds 3, 4.


Is there an example where I need to use exactly await Promise.all([store.add(1), store.add(2), tx.done]);?

Is transaction may fail after all write operations are successfully completed?

I looks it's enough to use await Promise.all([store.add(1), store.add(2)]); or await tx.done; (+1-2 ms) to be sure that the transaction is completed.

jakearchibald commented 2 years ago

Yes. done is the signal that the transaction completed and everything was written to the database. https://github.com/jakearchibald/idb#txdone