0

I am new to NodeJS/java Script. I would like to see how I can call a method synchronously. For example:

var getData = function (resourceUrl,  callback) {

//var done;

// Output representation
var summary = {

  title:"",
  };

// Request payload
var options = {

  uri: MyUrl,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key' : apiKey
  },
 json: {

      "articles": [
        {
          "type": "article",
          "url": resourceUrl,

        }
      ],


 }

};

request(options, function (error, response, body) {
        summary.title = body.title;
        return summary;
    }); // end of request function
}); // end of getSummary definition

Caller:

var output = getData('www.google.com'); // I need to wait here until getData is done. in C#, we use async/await
console.log(summarizedData);
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • There is no synchronous/blocking networking in node.js. Can't do it. Node.js is an event driven environment. Networking responses will come via asynchronous events. Learn to program that way or pick a different development environment with threads (such as Java) that lets you do synchronous networking. The node.js way of doing things is with callbacks or promises. – jfriend00 Oct 23 '16 at 17:37
  • Perhaps a dup of: [How do I return the response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – jfriend00 Oct 23 '16 at 17:45

0 Answers0