相关疑难解决方法(0)

Angular 4:如何在subscribe中使用await/async

我可以诚实地说,角度等待/异步真的是一个很棒的东西,它减少了很多大括号,提高了可读性并防止了很多人为错误.然而,有一件事困扰我很多.如何在subscribe中使用await/async.

让我们说吧

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

}
Run Code Online (Sandbox Code Playgroud)

TableCom是一个提供者,用作信号器组件和页面组件之间的通信器.

所以在页面组件构造函数中,它使用可观察主题从signalr组件接收新数据,如下所示.

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();
        });
    }
Run Code Online (Sandbox Code Playgroud)

问题是this.ReadTableAsync()基本上必须等待this.SaveTableAsync才能在启动之前完成.等待可以在这里实现吗?先感谢您 !!

typescript ionic-framework ionic3 angular

14
推荐指数
1
解决办法
5万
查看次数

标签 统计

angular ×1

ionic-framework ×1

ionic3 ×1

typescript ×1