Array changes not seen (by other threads?)
I have a recursive function which performs the following things(among others), in that order:
- Prints the array A which is passed as a parameter
- Concatenates some new values into it: A=A.concat(localList);
- Prints the array A again
- Runs a for loop, each iteration of which calls the function again
While the print sandwich shows correct concatenation, I notice that different(parallel?) instances do not aknowledge the changes other make. Aren't arrays passed as reference?
I've included minimal info because I feel this is some basic fact I'm missing.
Solution 1:
Array.concat returns a new instance. In order to keep the reference intact, you can concat a list like so:
yourarray.push.apply(yourarray, newitems)
Or more modern variant:
yourarray.push(...newitems)
To be clear, this will still not work if you're using multiple threads (Workers) since objects passed between Workers are cloned.