Node.js EventTarget 对比 DOM EventTarge
Node.js EventTarget 和 EventTarget Web API 之间有两个主要区别:
- 尽管 DOM
EventTarget实例可能是分层的,但 Node.js 中没有层次和事件传播的概念。 也就是说,调度到EventTarget的事件不会通过嵌套目标对象的层次结构传播,这些目标对象可能每个都有自己的事件句柄集。 - 在 Node.js
EventTarget中,如果事件监听器是异步的函数或者返回Promise,并且返回的Promise拒绝,则该拒绝会被自动捕获并按照同步抛出的监听器的方式处理(详见EventTarget错误处理)。
There are two key differences between the Node.js EventTarget and the
EventTarget Web API:
- Whereas DOM
EventTargetinstances may be hierarchical, there is no concept of hierarchy and event propagation in Node.js. That is, an event dispatched to anEventTargetdoes not propagate through a hierarchy of nested target objects that may each have their own set of handlers for the event. - In the Node.js
EventTarget, if an event listener is an async function or returns aPromise, and the returnedPromiserejects, the rejection is automatically captured and handled the same way as a listener that throws synchronously (seeEventTargeterror handling for details).