Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push to array safely, how to? worry or not?

I think this may be a naive question. I often find that I've got multiple xhr requests waiting on returns. I keep track of the requests I've issued in a javascript array. that is, when a new request is made, I add it to the array, then when the thread completes I remove it from the array.

In a multi-threaded OS, I'd be worried about threads crossing but I know javascript is single threaded. Should I worry about the array being consistent?

Sorry if this has been asked and and answered. I looked and did not find a link around array safe push.

like image 703
Peter Kellner Avatar asked Aug 28 '12 16:08

Peter Kellner


People also ask

Can you push to an array?

push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array.

Is JS array push thread safe?

No, you should not worry about it. JavaScript is single-threaded, like you said.

How do you push items into an array?

JavaScript Array push()The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.

Does push modify the array?

Examples of push in JavaScript and common errors Reassigning the array with the output from push is a common error. To avoid this error you need to remember that push changes the array, and returns the new length. If you reassign the variable with the return value from push() you are overwriting the array value.


1 Answers

No, you should not worry about it. JavaScript is single-threaded, like you said.

like image 110
Ry- Avatar answered Sep 18 '22 12:09

Ry-