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

109
Vistas
Parallel await calls of the same function override each other - Javascript

I wrote an API which gets called by multiple clients and computes their request. It roughly look like this:

async function main(request){
    url =  //extractUrlFromRequest
    answer = await compute(url)
}

async function compute(url){
    answer = await fetch(url)
    //other computations
    return answer
}

Keep in mind that the main function can be called multiple times per second or even at the same time by different clients and the computation time of the compute function may vary per request (up to ~2 seconds).

The Problem

The problem is that if a compute() call isn't finished before the next compute() call arrives the fetch() of the second call will override the fetch() of the first call no matter where in the code it currently is (The parameters in the header i.e. url do not change).

Example - Expected behavior

  1. compute() gets called the first time -> fetch() returns "hello" -> continues computation
  2. compute() gets called the second time -> fetch() returns "world" -> continues computation
  3. first compute() finishes -> returns "hello"
  4. second compute() finishes -> returns "world"

Example - Observed behavior

  1. compute() gets called the first time -> fetch() returns "hello" -> continues computation
  2. compute() gets called the second time -> fetch() returns "world" and replaces "hello" of the first call with "world" -> continues computation
  3. first compute() finishes -> returns "world"
  4. second compute() finishes -> returns "world"

What can i do to achieve the expected behavior?

What i've tried

I have read on MDN that Promise.all can be used to run multiple promises / awaits at once, however this isnt exactly my usecase since i dont have every request at the beginning, but they rather come in one after another and call the main() function again.

I've read that javascript is fundamentally single-threaded / linear, is it even possible to achieve the expected results, or did i choose the wrong language for this project?

about 4 years ago · Santiago Gelvez
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