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

194
Vistas
How to perform HTTP requests in NodeJS with a Priority value?

I have an array of objects which contains the url and priority number (which is an integer). I need to perform the http requests in ascending order of the priority value. For eg, if the array is like

const requestArray = [
  {
    url: "http://google.com",
    priority: 4
  },
  {
    url: "http://facebook.com",
    priority: 1
  },
  {
    url: "http://failed.example.gibberish.com",
    priority: 2
  }
];

First, the Facebook should be pinged as it's with priority 1 and a valid response to be logged (as it's valid) and failed.example.gibberish.com should be pinged as its priority is 2 and it should be rejected as its invalid URL and google.com should be pinged as it's with largest priority number value and whatever the response, it should be logged. I tried to achieve this with os.setPriority ( I am new to Node.js. I don't know how exactly to follow with this )

Demo: jdoodle.com/ia/jDx

In case of no priority value, like { url: 'http://jsonplaceholder.typicode.com/posts' }, I should set 1 to that particular object's priority value and it should come before any other request with priority greater than 1

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

0

Map missing priorities then sort your array by priority before executing your HTTP calls through a loop.

const requestArray = [
  {
    url: "http://google.com",
    priority: 4
  },
  {
    url: "http://facebook.com",
    priority: 1
  },
  {
    url: "http://failed.example.gibberish.com",
    priority: 2
  },
  {
    url: "http://nopriority.com"
  }
];

const cleanRequests = requestArray
  .map((req) => req.hasOwnProperty('priority') ? req : { ...req, priority: 1 })
  .sort((a, b) => (a.priority > b.priority) ? 1 : -1);

for (let request of cleanRequests) {
   const { priority, url } = request;
   console.log({ priority, url });
   // HTTP calls here...
}
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