Question:I am trying to synchronise a singleton. I would need to make this method like the equivalent of synchronized in java. What happens to me is that because the socket takes a while, if the first two requests are very close together I get two websockets created. (Then from the third one onwards it takes the instance correctly).
Answer:Below is an attempt to synchronize your singleton. The idea is to store the
o.intitializepromise and check if it already has been acquired before.
I added an
uid, a random value set in
initializeto show that only single instance is created.
Compare it to your version:
Hope this is what suits you.
If you have better answer, please add a comment about this, thank you!