init(asyncId, type, triggerAsyncId, resource)


  • asyncId <number> 异步资源的唯一 ID。
  • type <string> 异步资源的类型。
  • triggerAsyncId <number> 在其执行上下文中创建此异步资源的异步资源的唯一 ID。
  • resource <Object> 对代表异步操作的资源的引用,需要在销毁时释放。

当构造有可能触发异步事件的类时调用。 这并不意味着实例必须在调用 destroy 之前调用 before/after,只是存在这种可能性。

此行为可以通过打开资源然后在资源可以使用之前关闭它来观察。 以下代码片段演示了这一点。

import { createServer } from 'node:net';

createServer().listen(function() { this.close(); });
// 或者
clearTimeout(setTimeout(() => {}, 10));require('node:net').createServer().listen(function() { this.close(); });
// 或者
clearTimeout(setTimeout(() => {}, 10));

每个新资源都分配了一个在当前 Node.js 实例范围内唯一的 ID。

  • asyncId <number> A unique ID for the async resource.
  • type <string> The type of the async resource.
  • triggerAsyncId <number> The unique ID of the async resource in whose execution context this async resource was created.
  • resource <Object> Reference to the resource representing the async operation, needs to be released during destroy.

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));

Every new resource is assigned an ID that is unique within the scope of the current Node.js instance.