0

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);
        });
    });
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
zakariah1
  • 362
  • 1
  • 11
  • 1
    "For the love of god that is all there was 20 years ago. Why is this so obtuse?" — Because you are using the old http module from the dawn of Node.js instead of `fetch`. – Quentin Apr 03 '23 at 19:52
  • 1
    Doesn't look like the https.get() method returns a promise. If it did you could simply use async await to wait until the response is available. There is is SO question about making the https module work with async await here: https://stackoverflow.com/questions/52951091/how-to-use-async-await-with-https-post-request – Daniel Black Apr 03 '23 at 19:54
  • 1
    @DanielBlack — Yeah, but moving to `fetch` would be far simpler (at least in terms of writing code; it might require upgrading to a current version of Node.js). – Quentin Apr 03 '23 at 19:55
  • Does fetch really work. I have read through that other post a million times and it does not work. The value gets assigned in the callback, but if you actually want to use the value the undefined always gets set. – zakariah1 Apr 03 '23 at 19:58
  • @zakariah1 are you using async await with it? – Daniel Black Apr 03 '23 at 19:59
  • I can't post an answer as this has been closed, but if you try the following it should wait for the data to return before continuing (assuming the code is executed in an async function): `var response = await fetch(url,{method: 'POST', headers: {'Content-Type': 'application/json'}}) var data = await response.json();` – Daniel Black Apr 03 '23 at 20:02
  • I tried that and it runs, but the subsequent lines all execute before it finishes. – zakariah1 Apr 03 '23 at 20:03
  • Thanks for the fetch example. Give me 10 and I'll let you know. Thanks! – zakariah1 Apr 03 '23 at 20:05
  • Thanks for all the suggestions, but I'm using node v16.19.1 and so fetch won't work apparently. – zakariah1 Apr 03 '23 at 20:19
  • @zakariah1 you may be able to add a polyfill for fetch if you really want to try it out. This question has more details: https://stackoverflow.com/questions/48433783/referenceerror-fetch-is-not-defined – Daniel Black Apr 03 '23 at 21:01

0 Answers0