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

93
Views
Javascript promises catch block initiating when no error

I have created a function that makes a call to an api shown below. I am displaying the message from setMessage on the front end. For some reason when there is no error the .catch block message flashes in setMessage() and then the setMessage() finally ends with the correct message from .then().

I'm not sure why this is.

function handleCoupon(e) {
    e.preventDefault();
    setMessage("");
    setLoading(true);
    fetch(`${process.env.NEXT_PUBLIC_SERVER_API}/subscription/coupon/get`, {
      method: "POST",
      body: JSON.stringify({
        appliedCoupon: couponCode.toLowerCase().trim(),
      }),
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((data) => {
        console.log(data);
        if (data.coupon === true) {
          setMessage(data.message);
          setLoading(false);
        } else {
          setMessage(data.message);
          setLoading(false);
        }
      })
      .catch(
        (error) => console.log(error.message),
        setMessage("Something went wrong, please contact support")
      );
  }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The .catch only accept single function as parameter, and you are passing 2 of them:

  1. (error) => console.log(error.message)
  2. setMessage("Something went wrong, please contact support")

Try merging them into 1 function, e.g.

.catch((error) => {
   console.log(error.message);
   setMessage("Something went wrong, please contact support");
});
about 4 years ago · Santiago Trujillo 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!