Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Why does the table changes data some time after the promise has already been fulfilled?

When i run the code:

async function test(){
    let table=[];
    setTimeout(() => {
      table[0]='resolved';    }, 300);
    return table;
}

let result;

test().then(res=>{
  result=res;
  console.log("first  -" + result);
});

setTimeout(() => {
     console.log("second -" + result)
    }, 4000);

i expect to get:

first  - []
second - []

but the result is:

first  - []
second - ['resolved']

why does this happen? Since the function test has already been fulfilled when it gets to the first console.log, why does the result changes?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

JavaScript passes and returns by value, but the array [] is an object, and returning the result or assigning it to a variable doesn't make a copy of that object. All three names—table, res, and result—all refer to that same array object, so when the code in setTimeout changes the array, then you see it in result later on.

// primitives like numbers and strings pass by value
let string1 = "abc";
let string2 = string1;
string2 = "def";
console.log(string1 + " / " + string2); // abc / def

let number1 = 123;
let number2 = number1;
number2 = 456;
console.log(number1 + " / " + number2); // 123 / 456

// if you replace the reference to the array, then they coexist
let array1 = [234];
let array2 = array1;
array2 = [567];
console.log(array1 + " / " + array2); // 234 / 567

// but if you modify it in place, then there's only one object
// so the modification shows in both places
let array3 = [345];
let array4 = array3;
array4.push(678);
console.log(array3 + " / " + array4); // 345,678 / 345,678

Your function test returns an array object, and that object is the object passed to then and saved in result. However, the browser (or other environment) still runs the setTimeout code, changing the contents of the array even after the reference to the array was saved to res and result.

For this reason, you have to be very careful when working with an array you've received as a variable: If you need to ensure it doesn't change unexpectedly, you might need to make your own copy of the array. Otherwise, the caller might change the array out from under you, exactly the way that your setTimeout does in test.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda