Consider these two functions when i call getStatusAll(data)-
data=[[],['1'],['2'],['3']];
async function getStatusAll(data) {
console.log("In getStatusAll");
try{
let statusPromiseArray = data.map(async(value) => {
result= await this.fetchStatusDBs(value);
return result;
});
statusResolvedArray= await Promise.all(statusPromiseArray)
return statusResolvedArray;
}catch(err){
throw(err);
}
}
async function fetchStatusDBs(data) {
console.log("In fetchStatusDBs");
try{
//fetch status in dvf_req_id for an dvf_req_id
if(data.length==0){
console.log("1");
dvfStatus = await Promise.resolve("Disabled");
console.log("2");
trainingStatus = await Promise.resolve("Disabled");
console.log("3");
inferenceStatus = await Promise.resolve("Disabled");
}
else {
console.log("4");
dvfStatus = await Promise.resolve("Enabled");
console.log("5");
trainingStatus = await Promise.resolve("Enabled");
console.log("6");
inferenceStatus = await Promise.resolve("Enabled");
}
return [dvfStatus,trainingStatus,inferenceStatus];
}catch(err){
throw(err);
}
}
I am trying to resolve multiple Promises within a Promise.all but the results is unexpected. Actual Output-
In getStatusAll In fetchStatusDBs 1 In fetchStatusDBs 4 In fetchStatusDBs 4 In fetchStatusDBs 4 2 5 5 5 3 6 6 6 [["Enabled","Enabled","Disabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"]]
Expected Output-
In getStatusAll inside map In fetchStatusDBs 1 2 3 inside map In fetchStatusDBs 4 5 6 inside map In fetchStatusDBs 4 5 6 inside map In fetchStatusDBs 4 5 6 [["Disabled","Disabled","Disabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"]]
But changing fetchStatusDBs like this returns output in the correct format.
async function fetchStatusDBs(data) {
console.log("In fetchStatusDBs");
try{
if(data.length==0){
dvfStatus = "Disabled";
trainingStatus = "Disabled";
inferenceStatus = "Disabled";
}
else {
dvfStatus = "Enabled";
trainingStatus = "Enabled";
inferenceStatus = "Enabled";
}
return [dvfStatus,trainingStatus,inferenceStatus];
}catch(err){
throw(err);
}
}
Can somebody help me out?