Angular 4: How to use wait / async in a subscription

I can honestly say waiting / asynchronously in angular is really great stuff, it reduces the number of braces, improves readability and prevents a lot of human errors. However, one thing puzzles me a lot. how can i use wait / async inside a subscription.

let's say

 @Injectable()
export class TableCom extends BaseCom {
  public subject = new Subject<any>();

}

TableCom is a provider that serves as a communicator between the signalr component and the page component.

therefore, inside the constructor of the page component, an observable object is used to receive new data from the signalr component, as shown below.

constructor(protected nav: NavController,
        protected db: Storage,
        protected alert: AlertController,
        protected order: OrderData,
        protected translate: TranslateService,
        public navParams: NavParams,
        public toastCtrl: ToastController,
        private table_data: TableData,
        private load: LoadingController,
        private http: Http,
        private com_table: TableCom

    )
    {
        super(nav, db, alert, order, translate, undefined, false);
        this.previous_page = navParams.get('previous_page');
        this.subscribe_table = this.com_table.Receive().subscribe(res =>
        {
            await this.SaveTableAsync(res.data);
            this.ReadTableAsync();
        });
    }

the problem is that this.ReadTableAsync () should basically wait for this.SaveTableAsync should be finished before running. can I wait here? thanks in advance!

+6
1

"" async:

this.subscribe_table = this.com_table.Receive().subscribe(async res => {
    await this.SaveTableAsync(res.data);
    this.ReadTableAsync();
});
+6

All Articles