JS code execution, will there be half of the Function A () code execution, and then to execute the Function B () code?

as mentioned, we have the following business requirements

var x=[];

function A(){
X
}

function B(){
X
}

I"m worried now, because both function An and B are dealing with X. if there is a run-time crossover, it will cause some code logic problems.
in a database, it is generally in the form of a locked table to prevent X from being modified by both operations at the same time.
but in JS, is there any way to avoid this?
or, in other words, the way the JS code runs, this will not happen at all. It is certain that the complete code block will run before the next code block will be run.

Thank you

Mar.10,2021

No, single thread will not appear


without the concept of and , the code will be executed, but not necessarily the result (I think that's what you're asking). For example, if there is an asynchronous method in function , it appears that An executes to a certain part of execution B and then executes A.

var x = [];

function A() {
    setTimeout(() => {
        x[0] = 'A'
        setTimeout(() => { console.log('A', x) }, 0)
    }, 0)
}

function B() {
    setTimeout(() => {
        x[0] = 'B'
    }, 0)
}

A();
B();

the solution is the problem of asynchronous flow control. Search for a lot of it.

Menu