Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda