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

180
Visualizações
execute next request once the execution of first request is completed in node

Let's assume I have an endpoint /print. Whenever a request is made to this endpoint, it executes a function printSomething(). While printSomething() is processing, if the user or another user hits this endpoint it will execute printSomething() again. If this occurs multiple times the method will be executed multiple times in parallel.

app.get('/print', (req, res) => {
  printSomething(someArguments);
  res.send('processing receipts');
});

The issue with the default behavior is that, inside printSomething() I create some local files which are needed during the execution of printSomething() and when another call is made to the printSomething() it will override those files, hence none of the call's return the desired result.

What I want to do is to make sure printSomething() execution is completed, before another execution of printSomething() is started. or to stop the current execution of printSomething() once a new request is made to the endpoint.

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

0

You have different option based on what you need, how much request you expect to receive and what you do with those file you are creating

Solution 1

If you expect little traffic on this endpoint

you can add some unique key to someArguments based on your req or randomly generated and create the files you need in a different directory

Solution 2

If you think that it will cause some performance issue you have to create some sort of queue and worker to handle the tasks.

In this way you can handle how many task can be executed simultaneously

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are building a REST system with distributed calls this sounds problematic. Usually you don't want one request to block another. If the Order of operation is crucial (FIFO) then it looks like a classing Queue problem.

There are many different way to implement the Queue, you could use an array or something or implement a singleton class extending eventEmitter.

const myQueue = new Q()
const crypto = require('crypto');

const route = (req,res) => {
  const uniqeID =crypto.randomUUID()
  myQueue.once(uniqeID, (data) =>{
    res.send(data)
  })
  myQueue.process(uniqID, req.someDaTa)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You could use app.locals, as the docs suggest: Once set, the value of app.locals properties persist throughout the life of the application.

You need to use req.app.locals, something like

req.app.locals.isPrinting = true;
----
//Then check it
if (req.app.locals.isPrinting)

req.app.locals is the way express helps you access app.locals

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