Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda