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