Struggling to setup this function using async and await.
I have a function which maps over an object array. For each object, we make some external axios requests, receive a response before outputting an update object. I need to map over each object, update and return an array of updated objects.
Problem I am having, is that the output is not waiting for the resolved promise, resulting in undefined values being pushed to my array.
I note the array is also being logged before we handle each of the objects.
The following two functions are in question:
To build the object:
const createHRObj = async function (workflow) {
if (workflow.type_of_leave === "Personal") {
getAbsenceDetail(
`${workflow.employeeID}`,
`${workflow.startDate}`,
`${workflow.endDate}`
).then((res) => {
try {
console.log(`------- Handling Personal Leave! ----------`);
console.log(workflow);
if (res.Status != 0)
throw new Error(`PeopleHR absence status code: ${res.Status}`);
let absences = res.Result;
if (absences.length === 0) workflow.on_PeopleHR = false;
if (absences.length > 0) {
let count = 0;
absences.forEach((absence) => {
if (
Date.parse(absence.StartDate) <=
Date.parse(workflow.startDate) &&
Date.parse(absence.EndDate) >= Date.parse(workflow.endDate)
)
count ;
});
count > 0
? (workflow.on_PeopleHR = true)
: (workflow.on_PeopleHR = false);
}
console.log(`------- Absence Workflow Output! ----------`);
console.log(workflow);
console.log(
`------- Updating and returning workflow! ----------`
);
return workflow;
// console.log(`------- output from checkedarray ----------`);
// console.log(checkedArray)
} catch (err) {
console.log(err);
}
});
// Else, check for holiday
} else {
getHolidayDetail(
`${workflow.employeeID}`,
`${workflow.startDate}`,
`${workflow.endDate}`
).then((res) => {
try {
console.log(`------- Handling Business Leave! ----------`);
console.log(workflow);
if (res.data.Status != 10)
throw new Error(
`PeopleHR holiday status code: ${res.data.Status}`
);
let holidays = res.data.Result;
if (holidays.length === 0) workflow.on_PeopleHR = false;
if (holidays.length > 0) {
let count = 0;
holidays.forEach((holiday) => {
if (
Date.parse(holiday.StartDate) <=
Date.parse(workflow.startDate) &&
Date.parse(holiday.EndDate) >= Date.parse(workflow.endDate)
)
count ;
});
count > 0
? (workflow.on_PeopleHR = true)
: (workflow.on_PeopleHR = false);
}
console.log(`------- Absence Workflow Output! ----------`);
console.log(workflow);
console.log(
`------- Updating and returning workflow! ----------`
);
return workflow;
// console.log(`------- output from checkedarray ----------`);
// console.log(checkedArray)
} catch (err) {
console.log(err);
}
});
}
};
The second function, which calls the above function to handle the object creation is as follows:
const peopleHR_Check = async function (workflows) {
console.log(`----- We're now inside the PeopleHR Checker -------`);
Promise.all(
workflows.map(async (workflow) => {
let outputObj = await createHRObj(workflow)
return outputObj
})
)
.then(res => console.log(res))
};
CodePudding user response:
You need to return the Promise from the async
function createHRObj
:
return getAbsenceDetail(...).then(...)
However, it is simpler to use await
since it is an async
function for more linear flow.
const res = await getAbsenceDetail(...);
// use res (move the code in .then() here)
// return the final output at the end