• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

136
Views
Console.log Changes based on user input after being printed

I have this saga being called when opening a page with two console.log in to debug. This gives me two logs supposed to be similar. The values in data[0] is used in a AgGrid table. Two fields are editable in the table, these are null on the network call. However if i edit the fields in the browser, before expanding the console.log the value now shows as whatever i entered into the field. This will happen on either of the logs, so it can log the field as null first and then a value after.

function* saga({ Id }) {
    try {
        const response = yield call(get, GET_DATA(Id));
        console.log(response.data[0]);
        console.log(response.data[0]);
        yield put(fetchDataSuccess(response.data));
    } catch (error) {
        yield put(displayErrorMessage(NOB.ERROR));
    }
} 

Are console.log supposed to be able to change after being printed if what is references change? That would somewhat reduce how efficient it is for debugging.

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

console.log() is effectively async, and if the value you are outputting is an object that changes shortly after you call console.log(), it is likely that what actually gets output will be the updated version.

So, short answer to your question, yes.

If you need to output it around the time of changing, there are two relatively easy means you can use to output it at that stage.

The first would be to simply output a clone. You can create a shallow clone like this:

const objectToOutput;
console.log({ ...objectToOutput });

If you need a deep clone, you could pull in a library or just write your own method.

The second option would be to convert it to JSON first. You could even then convert that JSON back to an object and output that if you want. Handy for complex objects that contain simple data (like you'd get from an API call):

const objectToOutput;
console.log(JSON.stringify(objectToOutput, null, 2));
// or
console.log(JSON.parse(JSON.stringify(objectToOutput)));
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error