阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js async_hooks.createHook

      Node.js async_hooks.createHook(callbacks) 新增于: v8.1.0

      • callbacks < Object> The Hook Callbacks to register
        • init < Function> The init callback.
        • before < Function> The before callback.
        • after < Function> The after callback.
        • destroy < Function> The destroy callback.
      • Returns:Instance used for disabling and enabling hooks

      Registers functions to be called for different lifetime events of each async operation.

      The callbacks init()/before()/after()/destroy() are called for the respective asynchronous event during a resource’s lifetime.

      All callbacks are optional. For example, if only resource cleanup needs to be tracked, then only the destroy callback needs to be passed. The specifics of all functions that can be passed to callbacks is in the Hook Callbacks section.

      const async_hooks = require('async_hooks');
      
      const asyncHook = async_hooks.createHook({
        init(asyncId, type, triggerAsyncId, resource) { },
        destroy(asyncId) { }
      });
      

      Note that the callbacks will be inherited via the prototype chain:

      class MyAsyncCallbacks {
        init(asyncId, type, triggerAsyncId, resource) { }
        destroy(asyncId) {}
      }
      
      class MyAddedCallbacks extends MyAsyncCallbacks {
        before(asyncId) { }
        after(asyncId) { }
      }
      
      const asyncHook = async_hooks.createHook(new MyAddedCallbacks());
      

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


      目录
      本文目录
      目录