asyncFunc1 is the function I am waiting to complete.
const asyncFunc1 = async (data) => {
let money = data.money
let res = {}
if(data.flag === 1) {
let calRes = await funcToCalTax(data)
res.calRes = calRes
} else {
let calRes = await funcToCalBonus(data)
res.calRes = calRes
}
return res
}
const mainFunc = (data) => {
let totAmt = []
let empList = ['abc', 'def', 'ghi']
empList.forEach(element => {
totAmt.push(bonus(element))
});
if(data.tempFlag > 0) {
totAmt.push(asyncFunc1(data))
if(data.check === true) {
totAmt.push(func2(data))
}
}
return totAmt
}
Since asyncFunc1 returns a promise mainFunc needs to changed accordingly. I am getting stuck while converting it to accept that promise. This is what I have written for mainFunc:
const mainFunc = (data) => {
let totAmt = []
let empList = ['abc', 'def', 'ghi']
empList.forEach(element => {
totAmt.push(bonus(element))
});
if(data.tempFlag > 0) {
asyncFunc1(data).then(res => {
totAmt.push(res)
if(data.check === true) {
totAmt.push(func2(data))
}
})
// return totAmt
}
return totAmt
}
Where am I going wrong? Return is not waiting for the if part to be completed. How do I change it to return only after the if completes?