我可以诚实地说,角度等待/异步真的是一个很棒的东西,它减少了很多大括号,提高了可读性并防止了很多人为错误。然而,有一件事困扰了我很多。如何在subscribe中使用await / async。
让我们说吧
@Injectable()
export class TableCom extends BaseCom {
public subject = new Subject<any>();
}
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();
});
}
问题是this.ReadTableAsync()基本上必须等待this.SaveTableAsync才能在开始之前完成。等待可以在这里实现吗?先谢谢你 !!