I think it's a race condition with a network request callback and code that needs the result but is run outside the callback body.
I just tried debugging it in my devtools and the race condition is pretty easy to force / reproduce with breakpoints.
// No point in loading unread counts for anonymous users
// plus it helps us with not setting a prov cookie for 404 requests
var unreadCountData =
{
UnreadInboxCount: 0,
UnreadRepCount: 0,
UnreadNonRepCount: 0
};
if (!isAnonymous) {
$.get("/topbar/get-unread-counts", function (data) {
if (!data) return; // <- ADD BREAKPOINT HERE
unreadCountData = data;
});
}
inbox.handleRealtimeMessage(unreadCountData); // <- ADD BREAKPOINT HERE
achievements.handleRealtimeMessage(unreadCountData);
The code looks like it was written expecting unreadCountData = data;
in the callback to always complete before any calls to handleRealtimeMessage
functions.
To find that specific code, use the search box, or do a performance trace in your browser with screenshots and at the point where the bug happens, look for the call to handleRealtimeMessage
in the call-stack flame graph (This is for Chromium browsers. I'm not sure if the instructions are the same for Firefox).
Docs for jQuery get
(https://api.jquery.com/jquery.get/) state:
This is a shorthand Ajax function, which is equivalent to:
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
The rest of the followup (anything that depends on the response data directly or subsequently/transitively) needs to be put in the success callback.