Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Script ending with events pending. #2730

Closed
ghost opened this issue Feb 9, 2012 · 3 comments
Closed

Script ending with events pending. #2730

ghost opened this issue Feb 9, 2012 · 3 comments

Comments

@ghost
Copy link

ghost commented Feb 9, 2012

This might not be a bug I could just be misunderstanding.

When I run the bellow code it just exits at the end of the script. I thought though if there where events registered that the program would not just exit but continue to run.

var events = require('events');

var eventEmitter = new events.EventEmitter();

eventEmitter.on('someOccurence', function(message){
console.log(message);
});

@bnoordhuis
Copy link
Member

What keeps the event loop alive is handles (sockets, timers, etc.) of which your script has none.

EventEmitter instances are synchronous - that is, they run immediately - so in your example, once the event has fired, the script is done.

@ghost
Copy link
Author

ghost commented Feb 10, 2012

Got you. So holding on to resources outside of Node keeps the wheel turning. I was confused from the laymen's point of view it seemed to be being explained in the way that the event que listening to something registered to on would keep the script alive.

Thank you. I don`t completely understand why it works this way but at least I know how now I can dig into it more.

@bnoordhuis
Copy link
Member

that the event que listening to something registered to on would keep the script alive.

Think of it like this: an EventEmitter in itself isn't useful, it only becomes useful when it's tied to something that emits interesting events (data from the network or the file system, a timer that expires, etc.).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant