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

145
Vistas
Lookup map functions being called during declaration

I am trying to implement a switch statement alternative using a map object and the functions that are supposed to be called according to the condition are being called while I'm declaring the map

Here is a simplified code of what I implemented

someMethodName(someParams, otherParams) {
  const verifyThis = callExternalApi(someParams);

  const customSwitch = {
    oneValue: this.doThis(otherParams),
    anotherValue: this.doThat(otherParams),
  };

  const possibleValues = ["oneValue", "anotherValue"];
  if (possibleValues.includes(verifyThis))
    return customSwitch[verifyThis];
  return this.defaultCase(otherParams);
}

I put a console.log in the methods to be called and found out that they are all being called, supposedly while I'm declaring customSwitch, and then one of the methods are being called when going through the if clause.

How do I work around this to avoid calling my methods?

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

0

Use an object whose values are functions which, when called, invoke the other functions:

const customSwitch = {
  oneValue: () => this.doThis(otherParams),
  anotherValue: () => this.doThat(otherParams),
};

and invoke when returning by doing

return customSwitch[verifyThis]();
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