The code below declares an input_data as a const variable that is never modified in the lifetime of the execution. I expect every console.log to output the same thing 3 times but that is not the case.
This code is modified to be minimal and easily understandable and reflects the functions and promises am using in the real code. Could you tell me how and why this is happening and also show a working code with the same structure that doesn't modify that const variable
const platform = "test"; const input_data = { "test": true }; function verify(data) { let cmd = input_data; cmd.valid = true; return cmd; } console.log("#req", input_data); let command = verify(input_data); console.log("#req", input_data); new Promise((resolve, reject) => { command.executed = true; resolve("successs") }).then(result => { console.log("#req", input_data); }); Below is the output I get
#req { test: true } #req { test: true, valid: true } #req { test: true, valid: true, executed: true } Instead it should be like this
#req { test: true } #req { test: true } #req { test: true }
let cmd = input_datanow command is your input object as it is a refence to the original. When you then docmd.valid = trueyou added another key / value pair to the original object but you didnt violate the const.let cmd = {...input_data, valid: true}