Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async await in switch case statement don't work

can we wait for the resolution of a promise within switch case statement, using the keyword await? in my angular component I have the following code which crashes my application.

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']);
              }

After several tests, the error seems to me to come from lines starting with await. Can you tell me what's wrong please. I would like this.dbService.addOrderPaymentResult(ProcessResult.done, this.printerVMService.getSessionId()) which returns an observable is executed synchronously before going to this.router.navigate(['welcome/paying/paying_accepted']); that's why i chose to use toPromise() and await

like image 242
agnidé Avatar asked Feb 18 '20 10:02

agnidé


People also ask

Does await block execution?

await only blocks the code execution within the async function. It only makes sure that the next line is executed when the promise resolves. So, if an asynchronous activity has already started, await will not have an effect on it.

What happens if we execute an asynchronous method but don't await it?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Does await wait for all promises?

If a Promise is passed to an await expression, it waits for the Promise to be fulfilled and returns the fulfilled value.

What happens if await throws error?

If a promise resolves normally, then await promise returns the result. But in the case of a rejection, it throws the error, just as if there were a throw statement at that line. In real situations, the promise may take some time before it rejects. In that case there will be a delay before await throws an error.


1 Answers

Yes, you can:

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');
like image 194
TKoL Avatar answered Oct 01 '22 04:10

TKoL