NodeJS:同步调用方法(进而调用aysnc方法)

时间:2016-10-23 17:30:20

标签: node.js asynchronous async-await

我是NodeJS / java Script的新手。我想知道如何同步调用方法。 例如:

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

呼叫者:

var output = getData('www.google.com'); // I need to wait here until getData is done. in C#, we use async/await
console.log(summarizedData);

0 个答案:

没有答案