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

92
Views
Javascript promete iniciar el bloqueo de captura cuando no hay error

Creé una función que hace una llamada a una API que se muestra a continuación. Estoy mostrando el mensaje de setMessage en la parte delantera. Por alguna razón, cuando no hay ningún error, el mensaje de bloque .catch parpadea en setMessage() y luego setMessage() finalmente termina con el mensaje correcto de .then().

No estoy seguro de por qué es esto.

 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

El .catch solo acepta una sola función como parámetro, y está pasando 2 de ellos:

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

Intente fusionarlos en 1 función, por ejemplo

 .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!