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

93
Vistas
Promise chain of catchs

I have this scenario:

controller.ts

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

service.ts

someMethod(): ng:IPromise<void> {

    const deferred = this.$q.defer<void>();

    return this.OtherService.otherMethod()
    .catch ( e => {
        deferred.reject(reason);
    }
}

otherservice.ts

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

Test:

  • The otherMethod (otherService.ts) is getting an error from the HttpService.
  • The catch in someMethod (service.ts) is been executed.

Why, in the controller.ts, the then block is been executed?

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

0

The catch is executed if some previous then (or catch) throws an error. If no errors, the code will execute the next then statement.

So you have this code:

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
    });
}

So you can throw an error inside the catch. The code will continue to the next catch:

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

Why, in the controller.ts, the then block is been executed?

because you caught the error and returned undefined in service.ts

It looks like you should just get rid of the catch/defer entirely in service.ts if you don't plan on handling any errors in there.

EDIT: If you want the catch to be handled in the controller, then just remove all the stuff from service.ts and just let do:

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

If you want to handle the catch in service.ts AND in the controller, then rethrow the error (or a new one):

// 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 matter which you choose, you don't need a deferred.

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