Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda