Node.js event handling order

Given some EventEmitter instance in Node.js , is it absolutely guaranteed that all events will be handled in the same order as in the code?

var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;

var Emitter = function () {
    EventEmitter.call(this);
    var that = this;
    this.test = function () {            
        that.emit('eventA');
        that.emit('eventB');
    }
};
inherits(Emitter, EventEmitter);

var emitter = new Emitter();

emitter.on('eventA', function () {
    doTaskA();
});

emitter.on('eventB', function () {
    doTaskB();
});

emitter.test();

      

Could a situation arise when doTaskB()

will run before doTaskA()

?

+3


source to share


1 answer


Yes, all events will be processed in the same order as in the code. The event is triggered immediately after triggering the issue. You can see it in EventEmmiter.emit source code . But tying application logic to event ordering is not the best way.



+5


source







All Articles