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

88
Views
how to wait for promise to resolve in Javascript
const findNum = async ()=>{
                setTimeout(()=>{
                    return 5;
                },1000)};

const getNum = async ()=>{
                    const  num = await findNum();
                    return num;
                    }
                    
 const number =  getNum();

console.log(number);

number.then((result)=>{
                    console.log(result);
                    });

I tried so many answers from stack overflow,

like the "then" keyword, but it doesn't work!

The result I got was always like this or just some errors

Promise { <pending> }
undefined

how could I get the result out of the Promise?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The problem you have is that setTimeout has an callback and you return the value to the callback, not to findNum

setTimeout(()=>{
   return 5;
},1000)};

As you can see, there is an anonymous arrow function that you return you value to it.

The problem here is, you cant really make this work with async / await, you should use new Promise here:

const findNum = ()=> new Promise(res => setTimeout(res, 1000, 5))

This should work.

Also, async functions are always returning promises no matter what

about 4 years ago · Santiago Gelvez 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!