Search code examples
javascriptnode.jseventemitter

Do events fired using event emitter persist order


I have little nooby question about event emitter but it is really important for may program logic.

I am using some external library that fires events I am listening. Lets say it fires 2 events : 'data' and 'error'. Lets say that lib will always call data before error, something like this:

emit('data', 'some data');
emit('error', 'some error');

Question is: can I be 100% sure that data event will always come before error event in my listen methods?:

lib.on('data', function(data) {
            // is this always first
});

lib.on('error', function(error) {
            // or maybe this
});

Thanks,

Ivan


Solution

  • The EventEmitter emit function is a synchronous blocking function. And hence, like any other blocking function, it is guaranteed to execute in the exact order it is called in.