How does Javascript competition work?

Asked

Viewed 141 times

0

I have a possible problem in javascript.

The problem is that an array can have concurrent access because the following script below can be called at the same time several times because I use websockets.

for (let index in list) { // list é o array compartilhado
    if (list[index].id == id) {
        list.splice(index, 1);
    } 
}

In other languages, such as java, c, etc., you may happen to remove the wrong item from the list or even try to remove an item that no longer exists in the list, but in javascript is it possible? if not ,why? if yes, there is some way to treat it with other languages, something similar to mutex or semaphore?

  • "There is no competition" until you write your own logic to manage it, even in Java, the websocket only runs as a separate process, the way it receives is how it will work and if it is not handled will break, be it in Java, Javascript, Python, C++... Obviously such a list is available to everyone, the command that arrives first is the one that will be executed first, if you run the same command for a non-existent index in such a list probably something will fail.

  • The server sends various data to the client, the client receives the data and executes the script, as there is no competition?

  • 1

    Not quite what I meant, I ate a few words, in the sense there is no control over the competitive, in Java usually have Apis that facilitate this, identifying the "clients" and the like, including I created an HTTP server in Java with competition (control over this) and queue.

  • 1

    Complementing what @Guilhermenascimento explained, in Javascript there will be no external change in this list either by ajax or other function while that for run. Javascript only runs one process at a time and even what is "parallel" does not run in parallel, everything is sequential.

  • Thank you very much! was well clarified. Thanks to the 2

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.