0

I was asked a question in an interview. below is the question.

const JsonFromHTTPCall = function(){

    // make get request to some api url and return json object.
}



// code below is not editable

let result = JsonFromHTTPCall();
console.log("result ", result);

I am not finding a way to make console.log statement wait until I get the result from http call.

Please give me a way to solve it. Thanks in advance.

LokiKartik
  • 91
  • 10
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Shubham Dixit Jan 28 '20 at 06:52
  • Basically you have to make async to JsonFromHTTPCall and wait for it to complete. let result = await JsonFromHTTPCall(); – Sundar Singh Jan 28 '20 at 06:54

1 Answers1

1

Nodejs does not offer synchronous networking in any way. All built-in networking is asynchronous. Therefore, you cannot directly return a value from a function retrieved via networking. Instead, you need to communicate back the result either via a callback function, an event you trigger or a returned promise.

For a summary of this issue see this highly active question/answer:

How do I return the response from an asynchronous call?

There is a gross hack that involves using a synchronous child process and having it do the networking for you, but it's unlikely that is what they were asking for in your interview.

So, the main answer to the question is that "nodejs does not offer synchronous networking" and further "you cannot change an asynchronous result into a synchronous result". Therefore the proper way to code this is to use nodejs asynchronous coding techniques.

The cleanest way I know of to make http get calls is using a library such as request-promise() or my newer favorite got() and use the promise interface plus async/await to make a nice clean code path:

const got = require('got');

async function getSomeJSON(url) {
    let data = await got(url).json();
    console.log(data);
    return data;
}

getSomeJSON(myURL).then(data => {
    console.log("got my data");
}).catch(err => {
    console.log(err);
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I shouldn't change the function calling part. Please have a look at the question once – LokiKartik Jan 28 '20 at 06:59
  • @LokiKartik - Well, you CAN'T return an asynchronously retrieved value from a function directly. CANNOT do it except with the horrible hack I mentioned. I showed you the cleanest way to do it and a recommended way to do it. I think they must be testing if you understand how asynchronous operations work in nodejs and know how to code with them? – jfriend00 Jan 28 '20 at 07:12
  • @LokiKartik - See what I added to my answer to clarify. – jfriend00 Jan 28 '20 at 07:16
  • @LokiKartik - I fully looked at your question before I wrote my answer. You cannot do it that way. I wrote a way that you can do it. – jfriend00 Jan 28 '20 at 07:18