Will the following codes behaves different in asynchronous scenario?
a.unshift(x)
a = [x].concat(a)
How do I verify that?
Since unshift operates on the array directly, while the assignment =
will create a new array.
So I am wondering if they are running among massive code and there are asynchronous function that invoking them, will they behaves differently?
Will it happen to lose something when using .concat
?
Example:
let a = []
a = ['a'].concat(a)
a = ['b'].concat(a)
a = ['c'].concat(a)
// a = ['c', 'b', 'a']
Normally the above code are equivalent to the followings:
let a = []
a = a.unshift('a')
a = a.unshift('b')
a = a.unshift('c')
// a = ['c', 'b', 'a']
But will the later one be more secure? What if in some rare cases, while the a = [x].concat(a)
is still in progress, the other places changed the a
, and after the a = [x].concat(a)
completes, the previous change would be lost.
Will this happen?