I have this issue in nodeJS where the code below the call to 4 async functions is executed before the functions return any data. The code below should return the status of each call in json. How can I execute each function in sequence? So when I return the data to caller its with the updated status or error that happened in each function?
This is what i have now..
let idItem = "";
let statusInsert = "N/A";
let statusSms = "N/A";
let statusEmail = "N/A";
let msgErro = "N/A";
try {
let retorno = inserirEmergencia(req).then(
iar => {
console.log(iar);
idItem = iar.data.ID;
statusInsert = "OK";
}
);
} catch (e) {
//Error handling
idItem = "0";
statusInsert = "Erro";
msgErro += " > " + e.message;
}
let jsonResposta =
{
"idItem" : idItem,
"statusInsert" : statusInsert,
"statusSms" : statusSms,
"statusEmail" : statusEmail,
"msgErro" : msgErro
}
res.json(jsonResposta);
so, jsonResposta has the initial values, how can i execute four functions like that and be sure that at the end the value is the actual function returns?
async/awaitsince it looks like you're usingPromises (inserirEmergencia(req).then). This answer is actually quite close to what you're looking for - stackoverflow.com/a/60243171/5862900 or this stackoverflow.com/a/57486730/5862900