阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js async_hooks AsyncResource 类

      Node.js async_hooks AsyncResource 类

      JavaScript 嵌入的接口

      Library developers that handle their own asynchronous resources performing tasks like I/O, connection pooling, or managing callback queues may use the AsyncWrap JavaScript API so that all the appropriate callbacks are called.

      AsyncResource 类

      The class AsyncResource is designed to be extended by the embedder’s async resources. Using this, users can easily trigger the lifetime events of their own resources.

      The init hook will trigger when an AsyncResource is instantiated.

      The following is an overview of the AsyncResource API.

      const { AsyncResource, executionAsyncId } = require('async_hooks');
      
      // AsyncResource() is meant to be extended. Instantiating a
      // new AsyncResource() also triggers init. If triggerAsyncId is omitted then
      // async_hook.executionAsyncId() is used.
      const asyncResource = new AsyncResource(
        type, { triggerAsyncId: executionAsyncId(), requireManualDestroy: false }
      );
      
      // Run a function in the execution context of the resource. This will
      // * establish the context of the resource
      // * trigger the AsyncHooks before callbacks
      // * call the provided function `fn` with the supplied arguments
      // * trigger the AsyncHooks after callbacks
      // * restore the original execution context
      asyncResource.runInAsyncScope(fn, thisArg, ...args);
      
      // Call AsyncHooks destroy callbacks.
      asyncResource.emitDestroy();
      
      // Return the unique ID assigned to the AsyncResource instance.
      asyncResource.asyncId();
      
      // Return the trigger ID for the AsyncResource instance.
      asyncResource.triggerAsyncId();
      

      更多选项请参考:Node.js 异步钩子,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录