Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!