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

129
Views
how can i display my promise datas inside of a variable?

I want to store an object returned from a promise inside a variable.

I used then and catch to get the object but when I log the result of my object getting undefined.

Here is my code :

    let allStudents;
    const getStudents = async () => {
        try {
            const response = await Axios.get('/api/v1/student/')
            return response
        } catch (error) {
            console.error(error);
        }
    }
    
    let gsk = getStudents().then((res) => {
        allStudents = res.data
        return allStudents
    }).catch((err) => {
        console.log(err);
    })
    
    console.log(allStudents)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When using .then() on a promise, your callback is not going to be called right away. In particular, the function calling .then() will continue to execute, such that lines that come later in the function will be executed before your callback.

In your example, the assignment to allStudents does not happen until the callback executes, and this is not going to happen until after you have already tried to print out the value with console.log(allStudents).

let gsk = getStudents().then((res) => {
  // this code not executed until getStudents() complete
  allStudents= res.data
  return  allStudents
})

// this code is executed before getStudents() completes
console.log(allStudents)

In order to wait for the promise to resolve before continuing your execution, you could use await instead:

let res = await getStudents(); // do not continue until getStudents() completes
allStudents = res.data;
console.log(allStudents);
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!