2

:headache:

Reading a JS book. Came across the following classic race condition problem that reminds me of my college lessons on async programming and threading. It sure is a headache.

Thanking A.I. for explaining it to me further, because I was thinking that the value kept being updated with each fileName once resolved, but no:

async function fileSizes(files) {
let list = "";
await Promise.all(files.map(async fileName => {
list += fileName + ": " + (await textFile(fileName)).length + "\n";
}));
return list;
}

Problem:
1. list starts as an empty string.
2. For each fileName, you create an async function (inside map) that does:
list += fileName + ": " + (await textFile(fileName)).length + "\n";
3. Because all the async functions are launched at once (via Promise.all), they all reach their await quickly, and at that moment they all “remember” the same initial value of list (the empty string).
4. Then, whichever async call resolves last will overwrite list with its update. The previous writes get clobbered, since each one applied += to the old snapshot (""), not the already-updated string.

A.I.: Why is this a problem?
The root cause is mutating shared state (list) across async boundaries. Because += is not atomic, each async task doesn’t “see” the other updates — they all start from the same baseline.

Solution:
async function fileSizes(files) {
const lines = await Promise.all(files.map(async fileName => {
const length = (await textFile(fileName)).length;
return fileName + ": " + length;
}));
return lines.join("\n");
}

Each async task just computes its own independent result string. No shared state, no race condition.

Book lesson: computing a new value (though more expensive) is often less error prone than changing existing values (bindings in this case).

College lesson: remember that operations on a computer are not atomic. You always have things like the ALU, the registers and the values in between. Oh great, now I am reminded of atomicity in database transactions. :barfs: lol

Useful lessons. This is why Computer Science fundamentals are important. :)

Comments
  • 1
    This is actually basic as F :P And shame on you for doing .length + "\n" :P You're the one who is making all those JS is weird memes and promoting typescript huh :P

    Enough bashing, yeah, I'm very confident that AI makes me smarter / understand things more. AI can always explain stuff on my own level if it's too complex.

    Sure, in someway AI makes you lazy, but what is the difference with the moment that we got the bible, encyclopedia, then google. This is just a new iteration of the process. I always hated the joke "google made programmers equal". Don't think so. AI comes closer, but still, extremely no.
  • 0
    @retoor Basic for youuuu. lol. Rusty me needs to train this more. Why are you shaming me? This is text from a book. ;P Literally, including code.

    I see doctors using A.I. to help them understand the trickier things.

    I only used A.I. when I really wasn't sure what was going on and I wanted to confirm my suspicions. The latter engines just takes longer.
Add Comment