I think this code solve your issue , you have to wait until each request is completed
function pegaCaminho(id) {
return new Promise((resolve,reject)=>{
api.get(`/tratadadoscatdigito/caminho/${id}`)
.then(function (response) {
// handle success
//console.log(response.data);
resolve(response.data)
})
.catch(function (error) {
console.log(error);
reject(error)
});
})
}
const trataDadosCatDigitoComCaminho = async (req, res) => {
const string = dados;
const separaLinha = string.split(/\n/)
// you have to add async to each item
const item = await separaLinha.map(async (item) => {
const catName = item.match(/^\d+/)[0].trim()
const catNamePai = item.match(/\t\d+/)[0].trim()
const nome = item.match(/\t[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ].+/)[0].trim()
try{
var caminho = await pegaCaminho(catName);
}catch(error){
var caminho = null;
}
return { catName, catNamePai, nome, caminho }
});
return res.json(item)
};
1
solved Nodejs assync function return