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

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

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 Report

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