阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js async_hooks new AsyncResource

      Node.js new AsyncResource(type[, options])

      • type < string> The type of async event.

      • options < Object>

        • triggerAsyncId < number> The ID of the execution context that created this async event. Default: executionAsyncId().
        • requireManualDestroy < boolean> Disables automatic emitDestroy when the object is garbage collected. This usually does not need to be set (even if emitDestroy is called manually), unless the resource’s asyncId is retrieved and the sensitive API’s emitDestroy is called with it. Default: false.

      Example usage:

      class DBQuery extends AsyncResource {
        constructor(db) {
          super('DBQuery');
          this.db = db;
        }
      
        getInfo(query, callback) {
          this.db.get(query, (err, data) => {
            this.runInAsyncScope(callback, null, err, data);
          });
        }
      
        close() {
          this.db = null;
          this.emitDestroy();
        }
      }
      

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


      目录
      本文目录
      目录