Question

I am under the impression that I am using a wrong type of ''dispatch event''.

Now I want the commented-out code to run ONLY after x=5+5 was calculated and only after grawGrid(); finished whatever it is that it's doing.

I am under the impression that this falls under the async/sync realm of programming and from what I've read dispatch events are synchronous.

yoopee = function() {

    //Dispatch an event to another .js file.
    var eventFired = new CustomEvent("snapShotFired", { "detail": "An event was fired" });
    document.dispatchEvent(eventFired);

    //code here continues after the "x = 5+5 and drawGrid() finished calculating"
}

document.addEventListener("eventFired", function(e) {
  x = 5+5;
  drawGrid();
});

Am I using the correct type of event-dispatching, or am I missing the point here?

PS: The code above is not supposed to do anything, I'm just trying to get the point here.*

Was it helpful?

Solution

Per the MDN description of dispatchEvent():

the event handlers run on a nested callstack: they block the caller until they complete, but exceptions do not propagate to the caller.

Since they block the caller until complete, they run synchronously.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top