Hello. There is a workspace object and input JSON. You need to format and / or get the data and write it to this workspace. I use a loop to iterate over JSON elements, while the values that already exist, such as text, for example, go to the workspace, and the data that needs to be waited (get the exchange rate for example) comes after the execution of the cycle. For the last I use promises, but still, they come after. Help me to understand. Below is the code:
var checkJson = function(json) { return new Promise((resolve, reject) => { var workspace = {}; for (var i = 0; i < json.length; i++) { var id = json[i].id.match(/\D+/)[0]; switch (id) { case 'background': workspace['background'] = {}; workspace['background'] = createBackground(json[i], workspace['background']); break; case 'text': if (countert == 0) { workspace['text'] = {} } workspace['text'] = createText(json[i], workspace['text']); countert++; break; case 'time': if (countertime == 0) { workspace['time'] = {} } createTime(json[i], workspace['time']) .then(res => { workspace['time'] = res; countertime++; }); break; case 'currencycourse': if (counterCourse == 0) { workspace['currencycourse'] = {} } createCurrensyCourse(json[i], workspace['currencycourse']) .then(res => { workspace['currencycourse'] = res; counterCourse++; }) .catch(err => { console.log(err); }); break; // default: // console.log(itm_id); // break; } } Promise.all([workspace]) .then(values => { // console.log("values", values); resolve(values); }); }); }; The problem is with the exchange rate, the last resolve throws out the completed workspace, only currencycourse = {}
workspace['currencycourse'] = workspace['currencycourse'] || {}workspace['currencycourse'] = workspace['currencycourse'] || {}or, even better, initialize the object in advance - Daniel Khoroshko