[personal profile] sassa_nf
http://stackoverflow.com/q/36956220/1935631

It looks like the people don't even understand the meaning of the question.

Is there a safe pattern / good practice / guarantee by design on the arrival and observation of events?

$ cat a.js
'use strict';

const EventEmitter = require('events');

const e = new EventEmitter();
e.on('a', x => {
  console.log('x = ' + x);
  if (x === 0) {
    e.emit('a', 2);
    e.removeAllListeners('a');
    e.emit('a', 3);
    e.on('a', y => console.log('y = ' + y));
    e.emit('a', 4);
  }
});

e.on('a', z => console.log('z = ' + z));

console.log('a');
e.emit('a', 0);
console.log('b');
e.emit('a', 1);
console.log('c');

$ node a.js 
a
x = 0
x = 2
z = 2
y = 4
z = 0
b
y = 1
c



  • emit is atomic with respect to remove* and add* listeners
  • emits are not atomic with respect to each other
  • As a result, consumers can't agree on the observed order of events: x=0,x=2, but z=2,z=0 - but what's the way to make them agree? The only guarantee I can see is program order of emit start with respect to emit end (ie make all emits occur in a single place)
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

sassa_nf

February 2026

S M T W T F S
1234567
891011121314
15161718192021
222324252627 28

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 22nd, 2026 04:04 am
Powered by Dreamwidth Studios