Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
Map array of objects and change one property with a function that calls an API. I keep getting promise pending

I have to loop through an array of objects and modify one single property in each object. I modify this property with a function that connects to the Twitter API. My problem is that I must be using async and await wrongly because I am getting a promise pending.

This is my code:

getProfile:(req,res)=>{
        try {
           const userId=req.params.id 
           const profile=db.query('SELECT * FROM profiles WHERE user_id=?',
        [userId],async (err,result)=>{

            if(err) return res.status(404).send(err)

            const profiles= await result.map(  obj=>{

                const container={}
                container['name']=obj.profile_name
                container['desc']=obj.profile_desc
                container['twitter']=  connectTwitt.getTwitt(obj.twitt)//calls to api
                return  container
            })
            console.log(profiles)// promise pending
            res.send(profiles)

This is the structure of the array of object that I am mapping:

[
 {profile_name:`Elon Musk`, profile_desc:'enterpreneur',twitt:636465}
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes, you are using the async/await syntax a little bit incorrectly.

Right now, you are calling await on the Array.map() method. However, that method is not promise-based.

Instead, you have to add the await keyword to the getTwitt() method, and await for all promises to complete.

With those changes, it should look like below.

const profiles = await Promise.all(result.map(async (obj) => { // This line has been modified
  const container = {};
  container["name"] = obj.profile_name;
  container["desc"] = obj.profile_desc;
  container["twitter"] = await connectTwitt.getTwitt(obj.twitt); // This line has been modified.
  return container;
}));

Hopefully this helps with your <pending> issue!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda