I want to update the json object model values like the code below. But it does not seems to update the model when I change the values.
I have tried removing the async code, that seems to work. why async code does not to work? Can some one please explain me. Thanks
var json = {
lang: "es",
country : "Spain",
city : {
cityname : "name"
}
};
async function asynctranslateText() {
return new Promise((resolve, reject) => {
resolve("OK");
});
}
async function modifyJson(en) {
Object.keys(en).forEach(async function (item) {
if (typeof en[item] === 'object') {
await modifyJson(en[item]);
} else {
en[item] = await asynctranslateText();
}
});
}
(async () => {
await modifyJson(json);
console.log(json);
})();
Output
{ lang: 'es', country: 'Spain', city: { cityname: 'name' } }
Expected output:
{ lang: 'OK', country: 'OK', city: { cityname: 'OK' } }