socket.on('res', function(data) {
peanuts = data;
});
abc = function(){
peanuts = [];
socket.emit('req', index);
while (!peanuts[0]) {};
return peanuts[0];
};
like this, infinite loop.
abc = function(){
peanuts = [];
var intr = setInterval(function(){
if (peanuts[0]) {
clearInterval(intr);
};
}, 100)
return peanuts[0];
};
like this, it returns "peanuts[0]" before the "peanuts = data;"
pls help me.
socket.emit('req', index, function(answer) {
console.log(answer);
});
console.log('duck');
found that it could sending with acknowledgement.. but i want console.log(answer) run before console.log('duck')
OK,Actually my code is like this..
function poo() {
var x = a() & b();
/*
blablabla.....
*/
return z;
}
function a() {
socket.emit('req', index, function(answer) {
var temp = answer
});
return temp;
}
console.log(poo());
And if I use that ‘sync style’(but not sync), I need to rewrite function poo,right? That's why I want sync, for some reason, I can only rewrite function a, so, is it possible to check as the condition of while instead of emit with acknowledgement or using listener?
Like this?
function a() {
socket.emit('req', index);
while (!socket.???) {
};
return socket.????;
}