That's my problem, I have this function
function getStuff(query){
T.get('search/tweets', { q: query, count: 100 }, function(err, data, response) {
return data;
})
}
called inside a socket connection
net.createServer(function(sock) {
sock.on('data', function(data) {
var tweets = getStuff(request.query); <-- ERROR
sock.write(JSON.stringify(tweets));
});
}).listen(PORT, HOST);
My problem is that the variable tweets doesn't contains anything due to the async connection typical of node js. I'm started to study node js some days ago but for now I can't figure out how can I solve this problem. This is a typical programming pattern but how can I deal with this pattern in node js?
/* UPDATE */
function getStuff(query,callback){
T.get('search/tweets', { q: query, count: 100 }, function(err, data, response) {
callback(data);
})
}
called in this way
getStuff(request.query, function(tweets){
//Getting results
sock.write(JSON.stringify(tweets));
}
);