简体   繁体   中英

Why console.log goes infinite in async_hooks nodejs

Why console.log which is present inside init, before, after, destroy functions in async_hooks leads to infinite maximum call stack exceed problem?

Why does the below code goes infinite,

let asycn_hooks = require('async_hooks');

async_hooks.createHook({
  init(asyncId, type, triggerAsyncId) {
    console.log(asyncId);
  },
  before(asyncId) {
    console.log(asyncId);
  },
  after(asyncId) {
    console.log(asyncId);
  },
  destroy(asyncId) {
    console.log(asyncId);
  },
}).enable();
setTimeout(() => {
  console.log('>>>', async_hooks.executionAsyncId());
}, 10);

but the below code doesn't

let async_hooks = require('async_hooks');
let fs = require('fs');

async_hooks.createHook({
  init(asyncId, type, triggerAsyncId) {
    fs.writeFileSync(1,`init ${asyncId} \n`);
  },
  before(asyncId) {
    fs.writeFileSync(1,`before ${asyncId} \n`);
  },
  after(asyncId) {
    fs.writeFileSync(1,`after ${asyncId} \n`);
  },
  destroy(asyncId) {
    fs.writeFileSync(1,`destroy ${asyncId} \n`);
  },
}).enable();

setTimeout(() => {
    console.log('>>>', async_hooks.executionAsyncId());
  }, 0);

原因console.log 根据文档是异步

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM