If you aren’t doing fancy SQL queries and joins, which is the case for probably 95% of mobile apps, you might want to look into using ionic-storage, as the API is much simpler and, unlike direct interaction with SQLite, it works just fine in browser environments as well.
That being said, here are some rules I have made for myself when dealing with asynchronous code. In this case, connectToDb()
falls into the third category, so make the first word of it be return
(and I would get rid of async
).
I also don’t like “gatekeeper” variables like isConnectedToDB
. Everything that’s accessible should always be in a usable state. This thread describes an idiom that you might adapt, where db
is not exposed as a raw property, but rather as a Promise
itself.
In the long run, however, I think the safest, cleanest, and clearest way to do any of this is to never expose any details of storage implementation outside of service providers, so this whole connectToDb()
shouldn’t be callable from outside, if it even exists once we’re done refactoring things. When it comes to the notion of conditionally setting root pages based on external events such as user authentication state, I use this pattern, in which your app component doesn’t need to know or care how user authentication happens, just when it has.