V
V
vasIvas2015-01-01 21:09:48
Node.js
vasIvas, 2015-01-01 21:09:48

EventEmitter and a-synchrony?

Is it possible with EventEmitter.. I don't know how to say... to create an asynchronous chain of events?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Puzynya, 2015-01-02
@vasIvas

By itself, EventEmitter.emit is a synchronous function, which may surprise many.

process.on('ev', function() {
  console.log('ev');
});
(function() {
  console.log('a');
  process.emit('ev');
  console.log('b');
} ());
//a
//ev
//b

But that's where the synchronicity ends. Those. for example, if it process.on('ev', cb)was called not so trivially and in several places in asynchronous code, I would not advise you to even try to figure out which one cbwill be called first. I'm not saying that you can't calculate the call sequence, but I'm saying that calculating it is fundamentally wrong - it's an antipattern. If you want to be sure of the call sequence, use callbacks or promises, but not events.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question