Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!