I have a function that gets called through a socket.io event. This function is in another moldule. I want to make sure that everytime the function gets called the variables in the module are not changed by previous function calls.
I know how to do this in other languages: create a new instance of an object, call the function but i cant seem to get this to work in javascript.
The code looks like this:
socket.io module where the functions gets called
// -- all events -- //
io.on('connection', function (socket) {
console.log('user connected');
socket.on('increase', function (data) {
var increaser = require('./increase.js');
increaser.increase();
});
});
increase module, should print 1 everytime, but prints 1..2..3..4....
/*jslint node: true */
"use strict";
// -- variables -- //
var counter = 0;
module.exports = {
increase : function () {
counter += 1;
console.log(counter);
}
};
I want to know how to do this, because on my server a function gets called that calls a few asyncronous functions and i want to make sure that all variables stay like they are until the whole function is proccesed complete and dont get changed if another client connects and triggers the same event.