EventEmitter emit sync/async?

I am wondering if the emit method of the EventEmitter class operates synchronously or asynchronously.

For example, given that in both files eventEmitter is the same instance of the EventEmitter class:

// In file A

const myHandler = function() {
    console.log("Handler executed.");
}
eventEmitter.on("eventA", myHandler);

// In file B
eventEmitter.emit("eventA");

console.log("Event emitted.");

Is the order of the log:

> "Handler executed."
> "Event emitted."

always guaranteed?

Yes. emit() always works synchronously, it just loops through the registered callbacks and calls them.

2 Likes

Perfect, thank you @samme.

after years im still not sure what async is supposed to mean in most cases. Afaik phaser has a loopcycle like any animation on canvas would, wether that goes requestframe or whatever so everything is updated per-tick or per x-ticks (then again, im just a hobbyist, what would i know)