So in my code, I'd like to create a socket connection to my backend server:
socket = io('http://localhost:4000')
This goes well. However, immediately logging and returning the ID of said connection returns undefined
. If I place it all in a setTimeout
for 100ms all goes well. This, however, feels like a terrible solution. I've tried await socket = io('http://localhost:4000')
however this does not seem to work as this isn't a Promise.
Is there a way I can elegantly wait for the connection to be OK to proceed with the code? I've thought about immediately emitting a 'connected' event that the backend server replies to with a 'connected-confirmed' which triggers a listener that runs the rest of the code, however this is not elegant at all.
Thank you.