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

109
Visualizações
Javascript Return Value in graphql

I have an apollo-server setup in my backed, I want to use a post request API inside one of my resolvers and then use the response from that API to return it to my client. but the problem am having is the return statement is running before the API response get returned. there is my code sample bellow.

module.exports = {
    Mutation: {
        checkFace: async () => {
            console.log("Checking.....");

            let confidence;

            var parameters = {
              image_url1: "link to image 1",

              image_url2: "link to image 2",
            };

            facepp.post("/compare", parameters, function (err, res) {
                if (!err) {
                    confidence = res.confidence;
                } else {
                    console.log(err);
                }
            });

            return confidence
        },
    },
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's because the faceapp.post is probably running asynchronously. Your checkFace function is correctly used as async that's fine, but inside where you call the POST you should await the response and then return it

confidence = await res.confidence;

Also when using the function make sure you await for it to finish, so call it with

let someResponse = await Mutation.checkFace();
console.log(someResponse);

or

Mutation.checkFace().then(response => {
    console.log(response);
});

Whichever you prefer depending on your situation.

https://nodejs.dev/learn/modern-asynchronous-javascript-with-async-and-await

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