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

71
Views
Gestión de solicitudes mediante servicios

Noté que algunos desarrolladores están usando los llamados "servicios" para administrar las solicitudes de front-end, por ejemplo:

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

¿Hay alguna razón para crear múltiples servicios como: getPostService o getUserService , o getDataService , o la implementación es la misma si dentro del código es suficiente:

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

¿Es una razón para crear servicios o son redundantes en mi caso, siendo suficiente para obtener datos solo usando httpService sin crear muchos servicios en mi aplicación?

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

0

Su ejemplo es extraño y no tiene mucho sentido para mí.

No parece que su getPostService devuelva ningún servicio, literalmente solo envuelve la función httpService.get , sin ningún beneficio real.

Puede ver esto aún más claro al limpiar la función de:

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

A:

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

Sin embargo, si esta función se llamó getPosts o devolvió un objeto útil que realiza operaciones relacionadas con las publicaciones, tiene mucho sentido.

Por ejemplo, tal vez su PostService se vea así:

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

Ahora su servicio tiene algunos métodos que tienen un comportamiento útil y que se pueden reutilizar. Si varios de sus componentes necesitan una 'lista de publicaciones', y si todos esos componentes solo realizan solicitudes HTTP, entonces debe cambiar todos los componentes si desea cambiar algo sobre cómo se obtienen las 'listas de publicaciones'.

Poner esto en un lugar central aumenta la mantenibilidad

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!