I have a script where I am trying to look up a value using an API call. So I have an id and I want to make an apiCall
to retrieve a value. I retrieve the value just fine, but the value ends up being unassigned. I have gone through a huge number of posts, but most of them "cheat" in that they consider a console.log(value) a "win", when you know that any console.log("anything") will be written before the working example.
Basically, doing a callback works OK and you get the values just fine; however, how do you wait for the function to complete? I just need this to act as a procedure. For the love of god that is all there was 20 years ago. Why is this so obtuse?
"Why would I want to do this?" I think that this language should be able to "round up" different items into an object and then write the object to a file in a procedural way. Apparently, I just do not know how to pull it off.
data is an object
let value = apiCall(options, callback)
data.value = value; //Always undefined.
myWrite(data)
function myWrite(data) {
appendFile('./output/orm-layer-list.csv', data, function (err) {
if (err) { console.log("The following record had the error: ", err) }
});
}
function apiCall(options, callback) {
https.get(options, function (res) {
var body = "";
res.on('data', function (data) {
body += data;
});
res.on('end', function () {
let obj = JSON.parse(body)
callback(obj);
})
res.on('error', function (e) {
console.log("Error requesting data: " + e.message);
});
});
}