-1

I've module like following which I want to emit event like this:

file aa.js

var EventEmitter = require("events").EventEmitter,
    eventEmitterIns = new EventEmitter();
        
var preProcess = function (cb) {
    ...
    var start = function () {
        ....
     }).catch(function (err) {
        //-------------HERE I EMIT THE EVENT-----------
        eventEmitterIns.emit('test');
    }
  }
}

module.exports = {
      preProcess: preProcess,
      eventEmitterIns: eventEmitterIns
};

Now I want in other module to catch this error event when I do the following it doesn't work for me:

bb.js

var invokeAppEvents = require('../controller/aa');

    invokeAppEvents.eventEmitterIns.on('test', function () {
        debugger;

    }

);

If I put breakpoint it doesn't stops in the debugger; how can I overcome this?

The Code inside the catch block is called!

What should I export in aa.js and what should I require in bb.js?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
  • It doesn't look like your code is supposed to emit events. You just assign an emitter. – grabantot Feb 14 '16 at 15:31
  • @grabantot - in the catch I emit the event and I want to catch it in other module – 07_05_GuyT Feb 14 '16 at 15:32
  • In `bb.js`, do you ever actually call `preProcess` such that it triggers the `catch` of the promise? You haven't shown anything that does. We can help you better if you provide an [MCVE](/help/mcve) (C = "complete"). – T.J. Crowder Feb 14 '16 at 15:32
  • @T.J.Crowder - I dont use the preProcess I just want to listen to the event eventEmitterIns , and I know its inside the pre process but how can I overcome this? – 07_05_GuyT Feb 14 '16 at 15:34
  • give us more code so that we can see that `catch` is called. Or try putting breakpoints in the `aa.js` by yourself. – grabantot Feb 14 '16 at 15:36
  • @grabantot - the aa.js are called and I was able to enter to the catch block and emit the event but somehow it doesnt stops in the debugger – 07_05_GuyT Feb 14 '16 at 15:37
  • @shopiaT: *"I dont use the preProcess..."* Well, since `preProcess` is the only part of your code telling the emitter to emit an event, why are you expecting to get one?! – T.J. Crowder Feb 14 '16 at 15:38
  • @T.J.Crowder - so how should I catch this event in the bb module? – 07_05_GuyT Feb 14 '16 at 15:39
  • @shopiaT: You **are** catching the event. What you aren't doing is *emitting* it. if you don't call `preProcess`, nothing is ever calling `eventEmitterIns.emit('test')`. So, unsurprisingly, it never emits `'test'`, and you never see anything. *"The Code inside the catch block is called!"* **No, it isn't, not in the code in the question.** And that's what's wrong. If I take your code, fix the worst of the syntax errors, and actually use `preProcess` and tell it to reject a promise, I get the event, exactly as expected. – T.J. Crowder Feb 14 '16 at 15:41
  • @T.J.Crowder - No the preProcess is called after I run the server.js its called in the start of the program, when I put BP its stops in the catch block but it dosent stops in the on event.... – 07_05_GuyT Feb 14 '16 at 15:42
  • @shopiaT: Well, which is it? "the preProcess is called" (your comment [here](http://stackoverflow.com/questions/35393439/event-emitter-doesnt-emit-events?noredirect=1#comment58491050_35393439)) or "I don't use the preProcess" (your comment [here](http://stackoverflow.com/questions/35393439/event-emitter-doesnt-emit-events?noredirect=1#comment58490838_35393439))? **Again: [Provide a Minimal, Complete, Verifiable Example](/help/mcve)**. It'll only take you, what, three minutes, tops? Fundamentally EventEmitter works. Also share which debugger you're using and how you're calling `bb.js`. – T.J. Crowder Feb 14 '16 at 15:46
  • @T.J.Crowder - my question is what should I export in aa.js since I export both but in bb.js I need just to listen to the event... – 07_05_GuyT Feb 14 '16 at 15:58
  • @shopiaT: No, that isn't remotely what your question above is asking. If you want to ask that, feel free, but it's not what your question currently asks. I suggest deleting this, thinking hard about what you really are asking, and posting a **complete** question asking exactly what you're asking. Right now, this question cannot be usefully answered, and this back-and-forth isn't getting us anywhere. – T.J. Crowder Feb 14 '16 at 16:18

1 Answers1

0

You have to call preProcess() AND the inner start() function to get your event. Example:

aa.js

var EventEmitter = require("events").EventEmitter;
var eventEmitterIns = new EventEmitter();

var preProcess = function (cb) {
    var start = function () {
        Promise.reject('error...')
            .catch(function (err) {
                eventEmitterIns.emit('test', err);
            });
    }
    start(); // << start() be must called somewhere
}

module.exports = {
    preProcess: preProcess,
    eventEmitterIns: eventEmitterIns
};

bb.js

var invokeAppEvents = require('./aa');

// preProcess() must be called so start() can run
invokeAppEvents.preProcess();

invokeAppEvents.eventEmitterIns.on('test', function (data) {
    console.log('test event received: ' + data);
});

As expected, this outputs:

test event received: error...

Shanoor
  • 13,344
  • 2
  • 29
  • 40