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

147
Vistas
Potential race conditions when Promise used in subscriptions in Javascript / TypeScript

I recently dived into subscriptions of Subject/BehaviorSubject/ etc and I am looking for the goto approach when used in combinations with Promises.

Given is the example code below:

firebase.user.subscribe((user: any | null) => {
    fs.readFile('path/to/file')
         .then((buf: Buffer) => {
             this.modifySomeData = buf;
         });
});

I subscribe to a Subject that triggers whenever the user logs in or out of their service. Whenever this happens, I read a file from disk. This readFile event could potentially take longer than the next "login/logout" event. Of course, I am in JS and in an asynchronous environment. This means, my user code is not multithreaded, but still, the 2nd user event and 2nd readFile could theoretically be faster than the first readFile.

  1. First user event fired
  2. First readFile is executed
  3. Second user event is fired
  4. Second readFile is executed
  5. Second readFile is resolved <---
  6. First readFile is resolved <---

The order is mixed up. The silliest approach I could think of is to create a uuid before reading the file and check inside the promise if this is still the same. If it's not I discard the data.

Is there a better solution?

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

0

The first approach is to see if your event generation logic could handle waiting for event handling. For example, you can use a promise to wait for the event OR generate another event, say doneReadFile and only then send the next event. Usually, this is not the case for a generic (distributed) environment.

If event generation does not care about how long it took to handle events, you can still use the above approach but check for the intermediate event doneReadFile in the next event handler (login/logout). This can be achieved by implementing some kind of polling or busy-wait/sleep

about 4 years ago · Juan Pablo Isaza Denunciar

0

If i have a process where older requests can be discarded i often keep a variable in scope to track the latest request and compare, similar to your UUID idea:

let lastRead: Promise<Buffer> | null = null;

firebase.user.subscribe((user: any | null) => {
    const read = lastRead = fs.readFile('path/to/file');
    read.then((buf: Buffer) => {
        if (read != lastRead)
            return;

        this.modifySomeData = buf;
    });
});

In this specific case, readFile also supports an abort signal. So you might also be able to abort the last request instead; you will still need to track it though.

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