Skip to content Skip to sidebar Skip to footer

Node.js: Rest Client Returns The Value Before It's Returned

I am trying to use the node-rest-client REST client in Node.js. When I use the following code, it returns null but the console prints the response after that. How can I make synchr

Solution 1:

The module internally uses Node.js' native HTTP methods, so they aren't synchronous. You can't turn an asynchronous function into a synchronous one, so you need to use a callback:

var postRequest = function(url, args, callback) {
  var client = newClient();
  var responseData = {};
  client.post(url, args, function(data, response) {
    responseData = data;
    callback(responseData);
  });
};

Then you can call the function like this:

postRequest(url, args, function(response) {
  // response
});

Post a Comment for "Node.js: Rest Client Returns The Value Before It's Returned"