回调和承诺
🌐 Callbacks and Promises
quic 模块使用回调和 Promise 的组合来进行异步操作。例如,与 quic.connect() 建立连接会返回一个表示已建立会话的 Promise,而服务器端的传入会话则通过传给 quic.listen() 的回调来处理。在一个会话中,诸如传入流、数据报以及会话状态变化等事件通过 QuicSession 实例上的回调来处理。对于有明确完成点的操作,例如 TLS 握手完成或会话的优雅关闭,则使用 Promise。
🌐 The quic module uses a combination of callbacks and promises for asynchronous
operations. For example, initiating a connection with quic.connect() returns
a promise for the established session, while incoming sessions on the server
side are handled via a callback passed to quic.listen(). Within a session,
events such as incoming streams, datagrams, and session state changes are handled
via callbacks on the QuicSession instance. Promises are used for operations
that have a clear completion point, such as completion of the TLS handshake or
graceful closure of a session.
所有回调都是同步调用的,并且可以同步返回或返回一个 Promise。如果回调返回的 Promise 被拒绝,或者抛出错误,如果没有指定 onerror 回调,对象将会被销毁,并以该错误作为原因。
🌐 All callbacks are invoked synchronously and may either return synchronously or
return a promise. If a callback returns a promise that rejects, or throws an error,
the object will be destroyed with the error as the reason if an onerror callback
is not specified.