Debugging a Cached Profile Load Race in Event-Driven JavaScriptDebugging a Cached Profile Load Race in Event-Driven JavaScript
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
It worked on the first visit.
Imagine opening a customer profile. The details load. You go back to the list, open the same customer again, and this time the spinner just sits there. Clear the app’s cache and everything works again. That last bit would send me looking for bad cached data. The data could be perfectly fine.
Here’s how you get that bug with two innocent-looking lines. The first tells the app to load the profile. The second starts listening for the “profile ready” event.
On the first visit, there’s a network request. By the time the response comes back, the listener is in place and the profile appears. On the second visit, the app already has the customer in memory. If the loader fires “ready” immediately, the event is over before the second line runs. The spinner can wait all day.
I’d start with the smallest fix here. Attach the listener before asking for the profile. If I owned the loader and several screens used it, I’d also make its timing consistent. A cache hit can use queueMicrotask() to notify callers after their current code finishes.
What I like about this bug is how easy it is to hide during debugging. Clear the cache before every attempt and you keep testing the one path that works. Sometimes the most useful thing you can do is open the same customer twice.
I wrote up a small example you can paste into a browser console and follow one callback at a time. Read it on JS in Prod.

jsinprod.com

Why a Cached Callback and Its Promise Follow-Up Run Before a Timer

Trace a browser cache notification, its promise follow-up, a nested microtask and a zero-delay timer in one runnable example.

Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started