init(asyncId, type, triggerAsyncId, resource)
asyncId<number> 异步资源的唯一 ID。type<string> 异步资源的类型。triggerAsyncId<number> 该异步资源所在的执行上下文中创建的异步资源的唯一ID。resource<Object> 指向表示异步操作的资源的引用,需要在 destroy 期间释放。
当构造一个有可能触发异步事件的类时调用。这并不意味着实例必须在调用 destroy 之前调用 before/after,只是表示存在这种可能性。
🌐 Called when a class is constructed that has the possibility to emit an
asynchronous event. This does not mean the instance must call
before/after before destroy is called, only that the possibility
exists.
这种行为可以通过执行类似打开一个资源然后在使用该资源之前关闭它的操作来观察。以下代码片段演示了这一点。
🌐 This behavior can be observed by doing something like opening a resource then closing it before the resource can be used. The following snippet demonstrates this.
import { createServer } from 'node:net';
createServer().listen(function() { this.close(); });
// OR
clearTimeout(setTimeout(() => {}, 10));require('node:net').createServer().listen(function() { this.close(); });
// OR
clearTimeout(setTimeout(() => {}, 10));每个新资源都会被分配一个在当前 Node.js 实例范围内唯一的 ID。
🌐 Every new resource is assigned an ID that is unique within the scope of the current Node.js instance.