Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!