Fai*_*mou 8 javascript angular
我们可以使用关键字 await 在 switch case 语句中等待承诺的解决吗?在我的角度组件中,我有以下代码使我的应用程序崩溃。
switch (this.status) {
case 'opened':
break;
case 'payed':
this.getIntervalStatusSubscription.unsubscribe();
this.localStorageService.storePaymentIsMade(true);
await this.dbService.addOrderPaymentResult(ProcessResult.done, this.printerVMService.getSessionId()).toPromise();
this.router.navigate(['welcome/paying/paying_accepted']);
break;
case 'closed':
this.getIntervalStatusSubscription.unsubscribe();
this.paymentSessionIsClosed = true;
await this.dbService.addOrderPaymentResult(ProcessResult.error, this.printerVMService.getSessionId()).toPromise();
this.router.navigate(['welcome']);
break;
case 'used':
this.getIntervalStatusSubscription.unsubscribe();
this.router.navigate(['welcome']);
break;
default:
console.error('status don\'t exist');
this.utils.displayAlertError(this.device.getIntervalStatus);
this.router.navigate(['welcome']);
}
Run Code Online (Sandbox Code Playgroud)
经过几次测试,在我看来,错误似乎来自以await. 你能告诉我有什么问题吗?我想this.dbService.addOrderPaymentResult(ProcessResult.done, this.printerVMService.getSessionId())在去之前同步执行哪个返回 observable
this.router.navigate(['welcome/paying/paying_accepted']);这就是为什么我选择使用toPromise()和await
是的你可以:
async function trySwitch(status) {
switch(status) {
case 'opened':
console.log('1');
break;
case 'closed':
console.log('2');
await new Promise(r => setTimeout(r, 1000));
console.log('3');
break;
}
}
trySwitch('opened');
trySwitch('closed');Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11650 次 |
| 最近记录: |