alvardo
alvardo

Reputation: 157

Exception skipped Typescript Angular

I am trying to perform a validation and if no exception is thrown, then perform an action. The problem is that the code is async, so the "CompletePlanConfirm()" method is always run, but must be skipped if exception occurs. FirstCheck and secondCheck return Observable. is there a way to accomplish this?

completePlan() {
    try {
        this.validatePlan();
        this.completePlanConfirm();
    } catch (error) {
        throw error
    }
}

validatePlan() {
    this.planService.FirstCheck(this.plan.id).subscribe(
        data => {
            if (!data.result) {
                throw Error('Error 1')
            }
        });
    this.planService.SecondCheck(this.plan.id).subscribe(
        data => {
            if (!data.result) {
                throw Error('Error 2')
            }
        });
}

Upvotes: 1

Views: 212

Answers (2)

xinthose
xinthose

Reputation: 3820

I would use async and await and make your plan checks resolve promises. This makes your completePlan function a lot cleaner.

async completePlan() {
    try {
        const response1 = await this.checkFirstPlan();
        const response2 = await this.checkSecondPlan();
        
        this.completePlanConfirm();
    } catch (error) {
        console.error(error);
        throw error;
    }
}

checkFirstPlan(): Promise<any> {
    return new Promise((resolve, reject) => {
        this.planService.FirstCheck(this.plan.id).subscribe((data: any) => {
            if (!data.result) {
                throw new Error('Error 1')
            }
            resolve(data);
        }, (error: any) => {
          throw new Error(msg);
        }, () => { });
      });    
}

checkSecondPlan(): Promise<any> {
    return new Promise((resolve, reject) => {
        this.planService.SecondCheck(this.plan.id).subscribe((data: any) => {
            if (!data.result) {
                throw new Error('Error 2')
            }
            resolve(data);
        }, (error: any) => {
          throw new Error(msg);
        }, () => { });
      });    
}

Upvotes: 0

G&#233;r&#244;me Grignon
G&#233;r&#244;me Grignon

Reputation: 4228

You can use async / await to make your code synchronous.

Firstly change your validatePlan function so it retuns an observable (and some changes to avoid nested subscription :

validatePlan() {
  return this.planService.FirstCheck(this.plan.id).pipe(
    map(data => {
      if(!data.result) {
      throw Error('Error 1')
      }
    }),
    switchMap(() => this.planService.SecondCheck(this.plan.id))
  ).pipe(
     map(data => {
      if(!data.result) {
      throw Error('Error 1')
      }
    }),
  )
}

Then make completePlan an async function and place the await keyword before the validatePlan function (it needs to be changed into a Promise with .toPromise()

async completePlan() {

try {

  await this.validatePlan().toPromise();
  this.completePlanConfirm();
} catch (error) {
  throw error
}

This way the execution of the function will be synchronous and will wait for validatePlan before going further (so if an error is thrown it'll never try to confirm the plan)

Upvotes: 1

Related Questions