I want to define a function that gets me a list of certain IDs from a response of a GET request:
var getList = function (){
var list = [];
https.get(options).on('response', function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
var obj = JSON.parse(body);
for (i=0 ; i<obj.length ; i++){
list.push(obj[i].id);
}
//console.log(list);
//return list;
});
});
};
Now, I'd like to use that list from this function in other functions or simply assign it to a variable. I understand that since the function is asynchronous (well, the https.get one), returning the list won't mean much as the other code will not wait for this function to finish. Do I have to put all the remaining code inside the response.end call? I know I'm missing something very obvious here...