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

205
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

If you use TypeScript try decorators.

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

about 4 years ago · Juan Pablo Isaza Relatório

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