3
I have the following code:
const retorno_valores = []
result.forEach( value => {
var reader = getReader(conn, 10)
retorno_valores.push({reader})
});
const getReader= async (conn, cdReader)=>{
const dados = await med(conn, cdReader)
return dados
}
const med = ( conn, leitor ) => {
return new Promise((resolve, reject)=>{
conn.query('SELECT ip FROM reader WHERE ip = ?', [leitor], (error, result)=>{
if(error){
reject(error)
}
resolve(result[0].ip_controlado)
})
})
}
When I give a console.log('reader', reader)
in the variable reader
it shows as follows:
reader Promise { <pending> }
I would like to know how to get the value not to Promise
I’ve done it like this
const retorno_valores = []
result.forEach( async value => {
var reader = await getReader(conn, 10)
retorno_valores.push({reader})
});
But I can’t recover outside the foreach the array retorno_valores
.
You know what I found interesting? I just needed to add this here
const promessas = result.map...
and this hereawait Promise.all(promises);
and continue with my coderesolve(retorno_valores)
that worked... because I didn’t need the feedback?– adventistaam
@adventistaam because in a promise you do not need to return since the structure is not
topdown
and there is no way you can assess in this way that the execution was finalized– Sorack