Thus, the publish function returns immediately, in some way planning a given block of code, which should be executed immediately (asynchronously).
It seems that he notifies a bunch of listeners, so the author wanted to start the notification cycle later, and not block the publish code. Also note that the result of the notification (if any) is not required for the client code.
A side effect is that if one of the subscribers throws an exception, the publish method has no effect (another call stack).
This may not be idiomatic, but it is a fairly common pattern in JavaScript. Sometimes it is also used to trigger other events / timeout - especially in a very long-running function.
source share