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

263
Visualizações
Using one function that defines the get request every time i want to make a get request (best practice)?

i have Axios with all the interceptors. then i have api folder and put each function request base on api endpoint inside of it.

for exmaple :

api/user.js
api/posts.js
api/auth.js

and so on ... within the JS files i have multiple get request like this:

export getUsers = ({onSuccess, onError, onDone}) => {
 return Axios.get("Smaple/users/url").then(res => {
    if ( res && res.data ) {
      onSuccess(res.data);
    }
  }).catch(err => {
      onError(err);
  }).finally(()=>{
      onDone();
  })
}

i have multiple function like this one. then i notice i duplicating myself because body of the then, catch and finally are just like the others.

so is it ok to create a global get request like this below ?

const GET_REQUEST = (URL, {onSuccess, onError, onDone}) => {
 return Axios.get(URL).then(res => {
    if ( res && res.data ) {
      onSuccess(res.data);
    }
  }).catch(err => {
      onError(err);
  }).finally(()=>{
      onDone(err);
  })
}

and then user GET_REQUEST like so:

export getUsers = ({onSuccess, onError, onDone}) => {
   GET_REQUEST('Smaple/users/url', {onSuccess, onError, onDone})
}

this makes me think maybe later in future i may handle each of these request differently and it means my GET_REQUEST must have multiple if and else statements. and i think that is not good. is this a good practice or not ?

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

0

Following one of the Clean Code principles (I highly recommend you the book called Clean Code (Robert C Martin)), your first approach is the correct one. Try to follow the DRY Principle as much as you can, and avoid developing overthinking on how your code should behave in the future. This will aim you to cleaner and more readable solutions.

Your code is always up to be refactored by the time is needed. Do not hesitate to change your code once it is done. But I would let the code with just one function.

Regarding the fact of creating a global function, I would aim to create a class (Connector to your server) and define there this function.

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