Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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]();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda