How do i restructure following code? : <p>I want to create an json structure with data which will get from an api call. I can generate the structure by using following code. But how can I restructure the code to remove nested call of function and loops.</p>

<pre><code> var temp = {
    applications: []
};
    api.getApplications(conceptId)
        .then((applications) =&gt; {
            for (var i = 0; i &lt; applications.length; i++) {
                (function(indexOfAppArr) {
                    let applicationId = applications[indexOfAppArr].id;
                    temp.applications.push({
                        id: applicationId,
                        databases: []
                    });
                    api.getDbs(conceptId, applicationId)
                        .then(databases =&gt; {
                            for (var j = 0; j &lt; databases.length; j++) {
                                (function(indexOfDatabasArr) {
                                    let databaseid = databases[indexOfDatabasArr].id;
                                    temp.applications[indexOfAppArr].databases.push({
                                        id: databaseid,
                                        tabels: []
                                    });
                                    api.
                                    getSchema(conceptId,
                                        applicationId, databaseid).
                                    then(function(schemas) {
                                        for (var k = 0; k &lt; schemas.length; k++) {
                                            (function(indexofschemaarr) {
                                                let schemaid = schemas[indexofschemaarr].id;
                                                api.getTable(conceptId, schemaid)
                                                    .then(function(tables) {
                                                        console.log(tables);
                                                    })
                                            })(k)
                                        }
                                    })
                                })(j)
                            }
                        })
                })(i)
            }
        })
</code></pre>

<p>Here is the JSON structure which i want to create.</p>

<pre><code>    {
    applications:[{
        id:'',
        databases:[{
            id:'',
            tabels:[
                {
                    id:'',
                    columnId:''
                }
            ]   
        }]
    }]
};
</code></pre>
