在我的 Angular 2 应用程序中,我得到了方法:
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');
}
问题是:这个方法调用 console.log 的方式不是我想要的,我想先获取 console.log('first')
,然后在我得到模态确认后获取console.log('confirm')
,然后得到最终的console.log('finish')
;
现在我有这种情况,当我确认模态时,方法调用变量a
,然后是c
,然后是b
。
有人知道我该如何解决这种情况吗?
最佳答案
不是最适合使用 async await 的方式。但如果你必须的话。使用 toPromise
将 observable 转换为 promise,因为异步等待需要 promises 在幕后解决
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');
}
关于javascript - 在 Angular 2 中调用异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53914704/