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

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

If you use TypeScript try decorators.

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 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