EDIT: I want to explain the problem more clearly, my purpose is to make http response before "webot.waitRule()" returns. So should http request call back to webot.waitRule or the other way around?
below is my code which implements a module in a program, and my problem is the function returns before the http request get its response. I know http requests in node js is async, so there is any solution for it? I am pretty new in javascript and nodejs. Thanks.
webot.waitRule('wait_class', function(info) {
var courseName='lol';
var options = {
host: 'api.uwaterloo.ca',
path: '/v2/courses/CS/486/examschedule.json'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
response.on('end',function(){
// console.log(str);
// return "kkk";
var data = JSON.parse(str);
console.log(data['data']['course']);
courseName = courseName+data['data']['course'];
console.log("finished");
// return "lolllll";
// return "nide ke shi "+ courseName;
});
}
var req = http.request(options, callback);
req.end();
console.log("i am finshed");
return courseName;
});