We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
Simple and very useful, thanks for sharing!
this is really clear. thanks for the explanation!
Correct me if I'm wrong but If I do const dbOne = new DbClient();
const dbTwo = new DbClient();
I will have 2 different connections because inside the constructor you always set it to null
constructor() {
this.connectionPromise = null;
}
That means that this is not singleton
Great article! Thank you. Just wonder whether async is needed in the connect method as there is no await called in the singleton promise case? Cheers
What if it fails? The database will never be reconnected
Great point, you're right. This is actually discussed in my followup article here: https://www.jonmellman.com/....
There is still a bug in this code, I believe:
if (!this.connectionPromise) {
this.connectionPromise = connectToDatabase(); // stub
}
if connectToDatabase causes a context switch, then "this.connectionPromise" will still not be assigned, so a second call to getRecord will result in a second call to connectToDatabase()
connectToDatabase is synchronous, so the context switch you're describing isn't possible. Let me know if I'm misunderstanding.
Maybe you mean it's blocking, but connectToDatabase isn't synchronous, since it's a promise.
Absolutely great article. Thanks for sharing Jon
Jon thank you, great article!