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

204
Views
How to reuse conditional check for each function?

I have many functions example like this

function update() {
  if (isAdminUser()) {
    return false;
  }
  ...
}
function get() {
  if (isAdminUser()) {
    return false;
  }
  ...
}
...

is there any possible way to have the conditional statement

 if (isAdminUser()) {
   return false;
 })

written once and run by itself at the beginning of each function. I'm using javascript

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could use a higher order function to encapsulate the logic needed to run before a specific function is run. Higher order functions take functions as parameters, therefore a possible solution to your problem could look like this:

function withIsAdminUser(callback) {
  return function() { 
    if (isAdminUser()) {
      return false;
    }
    return callback();
  }
}

function getRaw() {
  // Do something here, this whole function could also be inlined
}
const get = withIsAdminUser(getRaw);
about 4 years ago · Juan Pablo Isaza Report

0

If you use TypeScript try decorators.

Docs: https://www.typescriptlang.org/docs/handbook/decorators.html#decorators

about 4 years ago · Juan Pablo Isaza Report

0

maybe you can define a function that can accept another function as an argument and returns false if that function (i.e isAdminUser in your code snippet) returns true

const checkUser = func => func() && false

then the function can be used like:

function update() {
    if (checkUser(isAdminUser)) {
        // update() logic will only run if user is not admin
    }

}
function get() {
   if (checkUser(isAdminUser)) {
      // get() logic will only run if user is not admin   
   }
 }
about 4 years ago · Juan Pablo Isaza 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!