阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js async_hooks 公共接口概述

      Node.js async_hooks 公共接口概述

      Following is a simple overview of the public API.

      const async_hooks = require('async_hooks');
      
      // Return the ID of the current execution context.
      const eid = async_hooks.executionAsyncId();
      
      // Return the ID of the handle responsible for triggering the callback of the
      // current execution scope to call.
      const tid = async_hooks.triggerAsyncId();
      
      // Create a new AsyncHook instance. All of these callbacks are optional.
      const asyncHook =
          async_hooks.createHook({ init, before, after, destroy, promiseResolve });
      
      // Allow callbacks of this AsyncHook instance to call. This is not an implicit
      // action after running the constructor, and must be explicitly run to begin
      // executing callbacks.
      asyncHook.enable();
      
      // Disable listening for new asynchronous events.
      asyncHook.disable();
      
      //
      // The following are the callbacks that can be passed to createHook().
      //
      
      // init is called during object construction. The resource may not have
      // completed construction when this callback runs, therefore all fields of the
      // resource referenced by "asyncId" may not have been populated.
      function init(asyncId, type, triggerAsyncId, resource) { }
      
      // before is called just before the resource's callback is called. It can be
      // called 0-N times for handles (e.g. TCPWrap), and will be called exactly 1
      // time for requests (e.g. FSReqWrap).
      function before(asyncId) { }
      
      // after is called just after the resource's callback has finished.
      function after(asyncId) { }
      
      // destroy is called when an AsyncWrap instance is destroyed.
      function destroy(asyncId) { }
      
      // promiseResolve is called only for promise resources, when the
      // `resolve` function passed to the `Promise` constructor is invoked
      // (either directly or through other means of resolving a promise).
      function promiseResolve(asyncId) { }
      

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


      目录
      本文目录
      目录