I write a function like bellow
function test() {
apiCall.ajaxCallWithReturnData(obj, 'GET', 'url')
.then(res => {
var metrics
metrics = res.Data.map((d) => { //filling metrics array
return {
MetricID: d.MetricID,
Metric: d.Metric
}
})
weeklyChartdata(null, 'Overall') //call method for default metric
.then(result => {
console.log('start')
})
.then(() => {
//console.log(scors)
metrics.forEach(function (m, i) {//Loop each metric
console.log('loop' + i)
weeklyChartdata(m.MetricID, m.Metric)
.then(result => {
console.log('loop' + i + 'result', result.scors)
})
})//Loop each metric
})
.then(() => {
console.log('done')
})
})
}
with above code I expect a result like bellow order
start
loop0
loop 0 result
loop1
loop 1 result
loop2
loop 2 result
done
but result is generate like bellow order
start
loop0
loop1
loop2
done
loop 0 result
loop 1 result
loop 2 result
I understand the metrics.forEach moving forward and not wait for rest code execution . please suggest me how I handle this situation ,to wait loop until all code executed.