I'm new to Node.JS and I started to read several topics about Node.js event handling and concurrency.
The bit of code following fires 1000 times an event, which is handled by incrementing a global variable. The first bit of code successfully counts to 1000, but the second bit doesn't even increment to 2. Is there any atomicity I don't get ?
I'm still wondering why the following code gives a coherent output (it counts successfully to 1000 without any concurrency:
// Import events module
var events = require('events');
// Event emitter
var eventEmitter = new events.EventEmitter();
var max = 1000;
var count = 0;
eventEmitter.on('event', () => {
let time = Math.trunc(Math.random() * 1000) +1;
setTimeout(() => {
var c = count;
c++;
count = c;
console.log(c);
}, time);
});
// Fire
for (let i = 0; i < max; i++) {
eventEmitter.emit('event');
}
console.log('Program Ended.');
Is there any atomicity I don't get ?
But when I move the var creation & assignation, the output is radically different (the final result is 1).
// Import events module
var events = require('events');
// Event emitter
var eventEmitter = new events.EventEmitter();
var max = 1000;
var count = 0;
eventEmitter.on('event', () => {
let time = Math.trunc(Math.random() * 1000) +1;
var c = count;
setTimeout(() => {
c++;
count = c;
console.log(c);
}, time);
});
// Fire
for (let i = 0; i < max; i++) {
eventEmitter.emit('event');
}
console.log('Program Ended.');
Any advice ? Any reading ?
Have a nice day !