I need help making my javascript asynchronous because I'm querying from a database with node. I'm using node.js and socket.io. The code works if I use setTimeout() functions, but I want to make my code more reliable so I'm trying to implement callbacks. However, I don't know how to use "socket.on()" properly in callback functions. I think the problem is that I'm nesting socket.on calls and the second "socket.on()" call doesn't execute. Any help would be greatly appreciated, thanks.
/*server side*/
socket.emit(variable1);//not important
socket.emit(variable2);//not important
//code above isn't important, I just wanted to show how data is used from my queries
/*client side*/
var socket = io.connect('123.456.789.123:3000');
var data = " ";//data I need to pass around
socket1(data, function(){
socket2(data)
});
function socket1(data, callback) {
socket.on('variable1', function (data) {
console.log("1");
callback();
}, callback)
}
function socket2(data) {
console.log("2");
socket.on('variable2', function (data) {
console.log("3");
})
}
The output is "12" and should be "123"