I have this axios call with a nested axios call that causes async problems. Here's a simplifyed snippet of my code:
axios.all([
axios.get('https://foo..', {
headers: { 'Content-Type': 'application/json' },
data: { 'endpoint': 'v2/bar..' },
}),
]).then(axios.spread((resp) => {
var resp = resp.data.content;
var items = resp.items.slice(0, 1);
items.forEach(item => {
var dataUrl = item.url.split('/v2/').slice(1).join('');
axios.get('https://foo..', {
headers: { 'Content-Type': 'application/json' },
data: { 'endpoint': 'v2/' + dataUrl + 'bar..' }
}).then(resp => {
const data = resp.data.content;
// alot of code goes here
}
)
}),
axios.get('https://foo..' + pageNum.toString(), {
headers: { 'Content-Type': 'application/json' },
data: {}
}
).then(resp => {
fillTemplate(resp.data.content[0].xml);
}
).catch(function (error) { console.log("DB-GET-Error:\n" + error) });
})
).catch(function (error) { console.log("error " + error) });
The problem is that 'fillTemplate()' executes before 'items.forEach(...)' is done and can populate 'fillTemplate()' with data, how can I solve this?