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

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 Report

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