1

In my angular 2 app i got method:

myMethod(component: any): void {
  let a = console.log('start');
  if (this._something) {
    // Here i got custom modal window
    this._service.callModalWindow('message').subscribe(result => {
      if (result === 'Confirm') {
        let b = console.log('confirm);
      }
    }
  }
  let c = console.log('finish');
}

The problem is: The way this method call console.log is not what i want, i want to get first console.log('first'), then after i got my modal confirmed get console.log('confirm'), and then get the final console.log('finish');

Now i have this situation, method calls variable a, then c, then b when i confirm modal.

Does anyone have any ideas how i can work around this situation?

5
  • Move let c right after let b ?! Commented Dec 24, 2018 at 14:32
  • @JonasWilms this is not a way, it was just a custom example to show my situation, i need to make this method exactly in this position Commented Dec 24, 2018 at 14:34
  • What's the difference between "confirm" and "finish" states? Also, console.log does not return anything. Commented Dec 24, 2018 at 14:34
  • Add everything out want to run as "finish.." in a seperate method and call it in subscribe after your "confirm" Commented Dec 24, 2018 at 14:36
  • stackblitz.com/edit/async-await-angular-demo Commented Dec 24, 2018 at 14:37

1 Answer 1

3

Not the most suitable way to use the async await. But if u must. use the toPromise to convert the observable to promise since async wait need promises to resolve behind the scene

async  myMethod(component: any): void {
  let a = console.log('start');
  if (this._something) {
  // Here i got custom modal window
  let result = await this._service.callModalWindow('message').toPromise();
  if (result === 'Confirm') {
    let b = console.log('confirm);
  }
  let c = console.log('finish');
}
Sign up to request clarification or add additional context in comments.

2 Comments

this is what i was looking for =)
return type is Promise<void> though.. good thing to know when calling it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.