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

113
Vistas
Typescript switch statement not working properly

For some reason this switch is not reaching any of the cases defined

const resultList = ['pass', 'fail', 'skip']
        for(const result in resultList){
            try {
                const endpoint = 'www.url.com';
                const response = await axios.get(endpoint);
                console.log(endpoint, response)
                if(response.status == 200){
                    console.log('status code good')
                    switch(result){
                        case 'pass':
                            console.log('case = pass');
                            this.passed = response.data;
                            break;
                        case 'fail':
                            console.log('case = fail');

                            this.failed = response.data;
                            break;
                        case 'skip':
                            console.log('case = skip');

                            this.skipped = response.data;
                            break;
                    }
                }
            }
            catch(error) {
                console.log(error)
            }
        }

I am seeing 200 responses from the API, however I never see any of the case logs... what am I doing wrong here?

Thanks in advance for the help

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The for...in loop iterate over the keys in a list, in this case:

const resultList = ['pass', 'fail', 'skip']
for(const result in resultList){ 
  console.log(result)
}

Will print

0
1
2

And for this, the condition on the switch fails.

To iterate over the values of a list, use the for...of loop.

for(const result of resultList) { ... }
about 4 years ago · Juan Pablo Isaza Denunciar

0

As expected; the solution was a minor change as highlighted by @kellys comment... the cases were never met because it was using the index as opposed to the value.

const resultList = ['pass', 'fail', 'skip']
        for(const result of resultList)
about 4 years ago · Juan Pablo Isaza 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