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 = {}

  • the problem should go away by itself if rewritten to async / await, since the code will expect rezolv promises in synchronous style - Daniel Khoroshko
  • By the way, countercourse counters are not needed, since you can write workspace['currencycourse'] = workspace['currencycourse'] || {} workspace['currencycourse'] = workspace['currencycourse'] || {} or, even better, initialize the object in advance - Daniel Khoroshko
  • You mean to describe at the very beginning that each workspace is a minimal reproducible example = workspace is a minimal reproducible example || {}? - Philip Pavlik
  • This is a standard js trick - if we are not sure whether an object exists, then we can assign it = ourselves || {} - Daniel Khoroshko
  • @DanielKhoroshko, Thanks, useful! - Philip Pavlik

1 answer 1

It seems so:

 async function checkJson(json): Promise<any> { const workspace = { background: {}, text: {}, time: {}, currencyCourse: {} }; for (const jsonNode of json.length) { const id = jsonNode.id.match(/\D+/)[0]; switch (id) { case 'background': workspace.background = createBackground(json[i], workspace.background); break; case 'text': workspace.text = createText(json[i], workspace.text); break; case 'time': workspace.time = await createTime(json[i], workspace['time']); break; case 'currencycourse': workspace.currencyCourse = await createCurrensyCourse(json[i], workspace.currencyCourse) break; default: console.log('default'); } } return workspace; }; 
  • but it is necessary to add a try-catch block either to asynchronous operations or a common one - Daniel Khoroshko
  • Thank you Works - Philip Pavlik