想在 event.emmit('eventName') 执行完异步事件之后 触发一个 end 事件,怎么做?
暴露 event 给 listener 吗?
const event = new (require('events').EventEmitter)
event.on('name',(event)=>{
// dosomething
setTimeout(()=>{
...
event.emmit('nameEnd')
},100)
})
event.on('nameEnd',()=>{
console.log(name)
})
event.emmit('name',event)
1
JayLin1011 2020-05-15 12:04:21 +08:00 1
const EventEmitter = require('events');
const event = new EventEmitter(); event.on('eventEnd', () => { console.log('異步事件執行後觸發 eventEnd 事件'); }); event.on('eventAsync', function () { setImmediate(() => { console.log('異步事件觸發'); // to do... this.emit('eventEnd'); }); }); event.emit('eventAsync'); |