If you’re writing asynchronous code – especially Javascript or Typescript – I have a story to share. It all started when I needed to initialize the Microsoft Teams JavaScript SDK for use in a web application I’m writing. It’s just a simple call in the browser:
await microsoftTeams.app.initialize();
This is all pretty easy, but the docs say you should only call initialize()
once and my app is a bit complex, with multiple web components that are rendered in parallel, and two of them need to use the Teams SDK on page load. So how can I prevent initialize() from being called more than once while isolating code within my web components?
Singleton promises
To prevent the multiple calls, I reached into my bag of geeky developer tricks and made this little function so it would only be called once:
Continue reading