I am trying to achieve the objective of performing tasks one after another means I want to wait for the one task until it gets completed and then following with the next task should execute. For that, I am using the async/await appropriately in order starting from loop to the functions which are being called. But what I see is the forEach loop is still executing before the current task finishes execution.
So this is my flow of code with the series of functions which are being called one inside other as well and I want the execution on async/await basis, but the loop is executing before happening that.
var lines = [
"GET_RELATIONSHIP Jnki Maternal-Uncle",
"GET_RELATIONSHIP Jnki Paternal-Uncle",
"GET_RELATIONSHIP Yodhan Maternal-Uncle",
"GET_RELATIONSHIP Laki Paternal-Uncle"
]
for await (const line of lines) {
var str = line.trim().split(' ');
if (str[0] == 'ADD_CHILD') {
await addChild(family.familyHead);
} else if (str[0] == 'GET_RELATIONSHIP') {
var inputName = str[1];
var inputRelationship = str[2];
console.log("str = " + inputName, inputRelationship);
var result = await getRelationship(family.familyHead);
}
}
const getRelationship = async (person) => {
if (inputRelationship == "Paternal-Uncle" || inputRelationship == "Maternal-Uncle" || inputRelationship == "Paternal-Aunt" || inputRelationship == "Maternal-Aunt")
await filterAllPaternalAndMaternalFromFamily(person);
if (person.children) {
person.children.forEach(getRelationship)
}
return true;
}
async function filterAllPaternalAndMaternalFromFamily(person) {
if (person.name == inputName) {
var takeAllPaternalOrMaternal = await getAllPaternalOrMaternal(person);
l = takeAllPaternalOrMaternal.length;
if (inputRelationship == "Paternal-Uncle" || inputRelationship == "Paternal-Aunt")
takeAllPaternalOrMaternal = await removePerson(takeAllPaternalOrMaternal, person);
if (inputRelationship == "Maternal-Uncle" || inputRelationship == "Maternal-Aunt")
takeAllPaternalOrMaternal = await removePerson(takeAllPaternalOrMaternal, person);
const male = await filterMale(takeAllPaternalOrMaternal);
const female = await filterFemale(takeAllPaternalOrMaternal);
if (l == takeAllPaternalOrMaternal.length)
console.log("None");
else if (inputRelationship == "Maternal-Uncle" || inputRelationship == "Paternal-Uncle") {
await printPerson(male);
} else if (inputRelationship == "Maternal-Aunt" || inputRelationship == "Paternal-Aunt") {
await printPerson(female);
}
} else if (person.wife == inputName || person.husband == inputName)
console.log('None');
}
async function getAllPaternalOrMaternal(person) {
var getUncleAunt = [];
const display2 = async (person2) => {
if (person2.father == person.grandfather && person2.mother == person.grandmother)
getUncleAunt.push(person2);
if (person2.children)
person2.children.forEach(display2);
}
await display2(family.familyHead);
return getUncleAunt;
}
async function filterMale(males) {
const male = await males.filter(uncle => uncle.gender === 'Male');
return male;
}
async function filterFemale(females) {
const female = await females.filter(aunt => aunt.gender === 'Female');
return female;
}
async function printPerson(person) {
var str = "";
if (person.length) {
person.forEach(async (item) => {
str += item.name + " ";
})
console.log(str);
} else
console.log('None');
}
This is the output I am getting which shows that the for loop has been executed even before the first whole sequence of tasks been completed.
str = Jnki Maternal-Uncle
str = Jnki Paternal-Uncle
str = Yodhan Maternal-Uncle
None
str = Laki Paternal-Uncle
Chit Ish Vich Aras
Vritha
None
None