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

65
Vistas
Request management using services

I noticed that some developers are using so-called "services" to manage front end requests, for example:

const httpService = {
  get(url) {
    fetch(url).then((response) => {
      if (!response.ok) {
        throw new Error('error')
      }

      return response.json()
    })
  }
}

const getPostService = (url) => {  // this is the service
  return httpService.get(url).then((json) => json)
}

getPostService('/post')
.then(r => setData(r))
.catch(e => setError(e));

Is there a reason to create multiple services like: getPostService or getUserService, or getDataService, or the implementation is the same if inside the code will do:

httpService.get(url).then((json) => setData(json)).then(e => setErr(e))

Is a reason to create services or they are redundant in my case being enough to fetch data only using httpService without creating many services in my app?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your example is strange, and doesn't make a ton of sense to me.

It doesn't look like your getPostService returns any service, it literally just wraps the httpService.get function, with no real benefit.

You can see this even clearer when cleaning up the function from:

const getPostService = (url) => {  // this is the service
  return httpService.get(url).then((json) => json)
}

To:

const getPostService = url => httpService.get(url);

However, if this function was called getPosts or it returned a useful object that does operations related to posts, it makes a lot of sense.

For example, maybe your PostService kinda looks like this:

class PostService {
  getPosts() {} 
  updatePost() {}
  deletePost() { }
}

Now your service has some methods that have useful behavior and that can be re-used. If multiple of your components need a 'list of posts', and if all those components just do HTTP requests, then you need to change all components if you want to change something about how 'lists of posts' are fetched.

Putting this in a central place increases maintainability

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