Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I have a question about the talk by Philip Roberts ("Help, I'm stuck in an event-loop").

In the following code, assuming that there are no read errors, is the load callback guaranteed to be always executed? I.e. are DOM events dispatched only after the call stack is cleared?

  let main = () => {
    let reader = new FileReader();
    reader.readAsText(file);
    reader.addEventListener("load", callback = (event) => console.log(event.target.result));
  };


Great question, and one i am not totally sure of myself. I guess the question is: if an event doesn't have any handlers listening for it, does it get queued and processed regardless? This is something I've often wondered about myself but never properly tested/been sure about.

My _guess_ would be yes, this would work. Regardless of how fast the file loaded. One way to test it would be to stick a large while loop between readastext and addeventlistener to block the stack for a few seconds and guarantee that the file is read before the handler is bound.

I'll try and take a look when I am back at a computer if nobody else responds either way :)


I'll add, most of my understanding was done this way: somewhat experimentally. Come up with a hypothesis of how things might work, figure out a way to prove it, and see if I was right.


Thanks for response! You are right, I have just done some tests with a time-expensive "for" loop placed right before the addEventListener() call and the callback appears to be fired after the stack is cleared / the main function returns.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: