Skip to content
Advertisement

socket.io – socket.on wait for promise

I have a button that does some communication with the server to check if an entered value (via an input box) already exists. The code is the following:

JavaScript

The problem is that the checkReturn call is asynchronous, and therefore the code carries on without actually waiting for the result. How do I make sure that checkReturn is first finished and only then the rest of the code gets executed?

Advertisement

Answer

Aside from the other answer, you can also use acknowledgements, where a callback is passed between the client and server. Then you can just use the callback of the emit function:

JavaScript

On the server side it would look like this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement