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

89
Vistas
Promesa cadena de capturas

Tengo este escenario:

controlador.ts

 methodA(): void { myServive.someMethod() .then( () => console.log("then") ) .catch( e => { console.log("catch"); }); }

servicio.ts

 someMethod(): ng:IPromise<void> { const deferred = this.$q.defer<void>(); return this.OtherService.otherMethod() .catch ( e => { deferred.reject(reason); } }

otroservicio.ts

 otherMethod(): ng.IPromise<any> { return this.HttpService.get(url); }

Prueba:

  • El otro método (otherService.ts) está recibiendo un error del HttpService.
  • Se ha ejecutado la captura en someMethod (service.ts).

¿Por qué, en el controlador.ts, se ejecuta el bloque entonces ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

El catch se ejecuta si algún then anterior (o catch ) arroja un error . Si no hay errores then el código ejecutará la siguiente declaración.

Así que tienes este código:

 methodA(): void { myServive.someMethod() .then( () => console.log("then") ) .catch( e => { console.log("catch"); // No errors thrown, so the code will continue in the next then }); }

Entonces puedes arrojar un error dentro del catch . El código continuará con la captura siguiente:

 methodA(): void { myServive.someMethod() .then( () => console.log("then") ) .catch( e => { console.log("catch"); throw new Error(e) // Some error happened! The code will continue in the next catch }); }
about 4 years ago · Juan Pablo Isaza Denunciar

0

¿Por qué, en el controlador.ts, se ejecuta el bloque entonces?

porque detectó el error y devolvió indefinido en service.ts

Parece que deberías deshacerte de catch/defer por completo en service.ts si no planeas manejar ningún error allí.

EDITAR: si desea que la captura se maneje en el controlador, simplemente elimine todas las cosas de service.ts y simplemente déjelo hacer:

 // service.ts someMethod(): ng:IPromise<void> { return this.OtherService.otherMethod() }

Si desea manejar la captura en service.ts Y en el controlador, vuelva a generar el error (o uno nuevo):

 // service.ts someMethod(): ng:IPromise<void> { const deferred = this.$q.defer<void>(); return this.OtherService.otherMethod() .catch ( e => { // you can either do: // throw e // which rethrows the same error (same as not having a catch in here at all) // or you can handle the error and throw a new one like: // // ...some error handling code // throw new Error('my new error'); }); }

No importa cuál elija, no necesita un diferido.

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