W
W
websiller2018-08-26 14:13:11
Node.js
websiller, 2018-08-26 14:13:11

Web socket asynchrony in nodejs. How to do it right?

There is this code:

ws.on('message', function(){
if(flag==true) {
flag=false;
// много кода
}
flag=true;
});

The task is this: it is necessary that the code in the callback is under no circumstances executed in parallel. That is, if the client makes one more request before the code in the callback is executed, then this second request should not execute the code in parallel. This is what the flag variable is used for. But this is just an example. Let's assume that requests from the client will come one after another almost at the same time, and the if(flag==true) check will be performed for both at the same time, even before the flag becomes false. In this case, the code will run in parallel. So the question is: maybe there are some tricks or additional functionality to prevent this.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question