Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
es6 map if the array gets changed
window.arr = [ 1, 2, 3, 4, 5 ];
window.arr.map(el=>{
    console.log(el);
});
asyncFunction(){
    window.arr = [ 2, 4, 6 ];
}

what will map do if asyncFunction happens to update the array while map is still running?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First off Array.prototype.map() is entirely synchronous so the single threaded nature of Javascript will prevent your asyncFunction from running during the time that your windows.arr.map() is running.

But, even more interesting is that window.arr = [2,4,6] assigns a new array to the window.arr variable. That array has nothing at all to do with the array that .map() was working on. So, even if they could somehow be interwined, the .map() operation has its own array and it just keeps working on that array and that array has nothing to do with the new array that was put into the window.arr property.

Remember that a statement such as:

 window.arr = [2,4,6];

doesn't do anything to any array that was previously in window.arr. If anyone else has a reference to the prior array, that array will still exist just fine and be unaltered.

Here's a simple demonstration:

let a = [1,2,3];
let b = [4,5,6];
window.arr = a;
console.log(a);
window.arr = b;
console.log(a);

From this, you can see that array a was not changed at all when window.arr was reassigned. So, in your example, it was array a that .map() was running on.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda