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

102
Visualizações
Nodejs async/await not working as expected

I have an express API that reads from my fauna database. I have a class setup called Database that handles all my fauna queries, and an async method called getQuotes that essentially gets a fauna document, the general structure is like this:

async getQuotes() {
    // note, `query` is an async function
    const doc = this.client.query(...).then((res) => {
        const data = ...; // to keep this short I'm not gonna show this
        console.log("faunadb handler: " + data); // just for debug
        return Promise.resolve(data);
    })
}

Then when I start the express API, I have it call the getQuotes method and log the data (just for debug).

const quotes = db.getQuotes().then((quotes) => {
    console.log("fauna consumer: " + quotes);
})

Now, when I run the app, I get the following output:

starting server on port.... ussual stuff
fauna consumer: undefined
faunadb handler: { ... }

The fauna consumer code runs before we actually get the promise from the fauna query API. I need to use .then because node for some reason doesn't allow me to use await. Does anyone know how to solve this? Thank you! (using node version v16.14.0, running on Arch Linux)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You aren't returning anything from your getQuotes() function. The return in your callback is only going to return from the callback, not from the enclosing function. You also aren't awaiting anywhere, which means your async function is not going to actual wait on the result of your query.

about 4 years ago · Juan Pablo Isaza Relatório

0

From my perspective it's better not to mix up await and then. Either use one or another.
I'd recommend to go with await.

async getQuotes() {
    // note, `query` is an async function
    const res = await this.client.query(...)
    const doc = ...; // to keep this short I'm not gonna show this
    console.log("faunadb handler: " + doc); // just for debug
    return doc;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

try to use await

async getQuotes() {
    const res = await this.client.query(...);
    const data = ...;
    console.log("faunadb handler: " + data); // just for debug
    return data;
}
// must be in async function
const quotes = await db.getQuotes();
console.log("fauna consumer: " + quotes);
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