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

313
Views
¿Qué devuelve una Promesa protegida por un catch()?

Tengo una función asíncrona que se llama al comienzo de mi código, y solo cuando se hace, pueden suceder más cosas. Normalmente sería algo como

 const initialCheck = () => { return fetch('https://reqres.in/api/users') .then(r => { // do things return r }) } const actualStuff = () => { console.log('hello') } initialCheck() .then(r => { actualStuff() })

El objetivo de esta verificación inicial es asegurarse de que el punto final HTTP ( https://reqres.in/api/users ) sea realmente accesible. Por lo tanto, debería tener en cuenta un problema de conexión y mi solución fue catch() el posible error:

 const initialCheck = () => { return fetch('https://thissitedoesnotexistihopeatleast') .then(r => { // do things when the endpoint is available return r }) .catch(err => { // do something when the endpoint is not availble // nothing is returned }) } const actualStuff = () => { console.log('hello') } initialCheck() .then(r => { actualStuff() })

Mi pregunta: ¿por qué funciona el código anterior?

catch() se ejecuta (y el then() en esa función no lo es), no devuelve nada y, a pesar de eso, .then(r => {actualStuff()}) genera lo que se espera ( hello en la consola).

¿Qué recibe then() realmente? (que no return )

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Un .catch encadenado a una Promesa resultará en una Promesa que:

  • se resuelve con el valor devuelto por .catch , si .catch regresa normalmente
  • rechaza con el error, si el .catch mismo arroja un error

Asi que

 const chainedPromise = somePromise.catch(() => { // no errors here // nothing returned })

Si .catch definitivamente no arroja, entonces chainedPromise definitivamente se resolverá (y no rechazará), y dado que no se devuelve nada, chainedPromise se resolverá como undefined .

Asi que

 initialCheck() .then(r => { actualStuff() })

funciona porque initialCheck devuelve una Promesa que resuelve (a undefined ).

Si devolvió algo del .catch y se ingresó el .catch , lo verá en .then encadenado a él más tarde:

 const initialCheck = () => { return fetch('https://doesnotexist') .catch(() => { return 'foo'; }); } const actualStuff = (r) => { console.log(r); console.log('r is foo:', r === 'foo') } initialCheck() .then(r => { actualStuff(r); })

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!