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

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

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