I have never heard of the saying that js has multiple processes, and it is impossible to modify an array at the same time. You can post the code for other experts to take a look
A simple experiment is as follows. When multiple processes (tasks) operate, the value of the array depends on the last process. I use 2 setTimeout and 1 setInterval to simultaneously operate an array, and push into three values 1, 2, and 3 respectively. The final result is:
[1, 2, 3, 3, 1, 1, 3, 1, 1, 3]
There is a clear explanation in Ruan Yifeng’s article: Single thread means that all tasks need to be queued, and the next task will not be executed until the previous task is completed. If the previous task takes a long time, the next task will have to wait. ;View original text
I have never heard of the saying that js has multiple processes, and it is impossible to modify an array at the same time. You can post the code for other experts to take a look
You should write it in NodeJS
A simple experiment is as follows. When multiple processes (tasks) operate, the value of the array depends on the last process. I use 2
setTimeout
and 1setInterval
to simultaneously operate an array, andpush
into three values 1, 2, and 3 respectively. The final result is:There is a clear explanation in Ruan Yifeng’s article: Single thread means that all tasks need to be queued, and the next task will not be executed until the previous task is completed. If the previous task takes a long time, the next task will have to wait. ;View original text
Just test it and you will know. Print this array every time the push is successful