search

Home  >  Q&A  >  body text

node.js - 关于一个events的问题,这段代码会无限循环吗?

ringa_leeringa_lee2867 days ago628

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 16:19:55

    const EventEmitter = require('events');
    
    let emitter = new EventEmitter();
    let n=1;
    emitter.on('myEvent', () => {
        console.log(n++);
        ***emitter.emit('myEvent');***
    });
    
    ***emitter.emit('myEvent')***;

    There are two emitter.emit('myEvent');

    Call event, the event itself also has a calling event, which creates a cycle. Finally, the call stack size is consumed and terminated.

    For reference.

    Love MongoDB! Have Fun!

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:19:55

    It will loop infinitely until the stack overflows... and then the process will be killed. Then there is no more...

    reply
    0
  • Cancelreply