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

106
Visualizações
Why is my catch statement triggering even though the .then is resolved?

This is the code I have currently in my server folder. I was just wondering for some reason the catch console log is still sending even though the .then statement actively sends a response though res.send works and sends a response to the user?

const express = require("express");
const router = express.Router();
const { API_KEY, API_URL } = process.env
const axios = require("axios")

router.get("/", async (req, res) => {
    console.log(API_URL + API_KEY)
    await axios.get(API_URL + API_KEY + "&count=9")
        .then((response) => {
            res.send(response.data)
        })
        .catch(
            console.log("error")
        )
})

module.exports = router

Thank you!

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

0

.catch accepts a callback - right now, you're invoking console.log immediately and passing the result to .catch. This:

// ...
.catch(console.log("error"))

is equivalent to

const consoleResult = console.log("error");
// ...
.catch(consoleResult)

You need to change to

.catch(
    () => {
        console.log("error")
    }
)

Or, even better, examine the argument to see what the error was:

.catch(
    (error) => {
        console.log("error:", error.message)
    }
)
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