This question has been asked in various ways already here, and I understand the problem pretty well. The general solution is to invoke callbacks for handling HTTP results, but I'm specifically wanting to avoid that.
I imagined I could use a "are we done yet" flag to keep things working, but this isn't working as expected.
//
// Test the IP the comment came-from.
//
exports.testJSON = function ( obj )
{
var ip = obj['ip'] || "";
var result = null;
var done = false;
//
// The URL request we're going to make
//
var options = {
host: 'www.stopforumspam.com',
port: 80,
path: '/api?ip=' + ip
};
//
// A GET request
//
var re = http.request(options, function(res) {
var str = '';
res.on('data', function(chunk) {
console.log( "Got data " + chunk );
str += chunk;
});
res.on('end', function() {
console.log( "Got END" );
result = "..... ";
done = true;
});
}).on('error', function(e) {
done = true;
console.log("Got error: ", e);
});
re.end();
while( ! done ) {
}
return( result );
};
Sadly this doesn't work - the busy look just spins indefinitely, and I see no console-logging to indicate that I'm receiving data.
Adding in "process.nextTick()" to the "while(!done){}" loop makes no difference either.
Surely I don't need to rework my whole plugin system to cope with a different approach, and the callback updating the "done" flag will work, somehow?