Open mikemorton opened 4 years ago
Re-reading the best practices docs I now see that using browser native Promises do not break out of scope, by design! That's cool and impressive.
Anyways, I do feel like there was somewhere else I read that made me feel that this shouldn't work. If I can dig it up I'll post here.
yes it used to be so in version 1.x. Docs have been updated in 2.0
Thanks for the reply @dfahlander - I'm curious how this works! Could you point me to the portion of the code that is responsible for keeping the transaction alive in this case?
Just to clarify two different issues:
In Dexie version 1.x, you had to stick to using Dexie.Promise for both these reasons. In Dexie version 2, zones will also temporarily replace the global Promise with Dexie's Promise - only for the code that is executing within the zone - so that the global promise becomes both IDB compliant on old browsers and in IndexedDBShim, and continues to maintain the zones. Note that the global Promise is always restored whenever leaving a zone - so we don't affect other code than what's executed in the transaction scopes. This is what makes the global Promise possible to use in Dexie >= 2.0, and also recommended in the docs.
Then comes a third issue: native async/await - which uses the native global internally - no matter if you've replaced the global promise or not. This is also worked around in Dexie using my own invention that I call "zone echoing". I will not go into it too much here. However, zone echoing does only maintain zones across await calls and cannot fix the first issue of old browsers and IDBShim that doesn't play well with promises. However, as said before, modern browser don't have that issue anymore.
The code that handles all these three things is in src/helpers/promise.js (one of the few modules that hasn't been converted to typescript yet).
If your intent is to break out of the transaction zone, use [Dexie.ignoreTransaction()](https://dexie.org/docs/Dexie/Dexie.ignoreTransaction()).
The following is generating an error about using a read-write transaction inside a read-only transaction
This generates an error:
I've read the transaction documentation and it sounds like I can just put a
!
or a?
on the READWRITE transaction in order to get what I want, but I must say this behavior was surprising because I thought that the Promises would break me out of transaction scope.Questions:
Promise.all
break me out of the transaction scope?doStuff
function break me out of the transaction scope?Promise.all
with a bunch of toArray() calls inside?