Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!