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 synchronized calls using the REST client?
var postRequest = function(url, args) {
var client = new Client();
var responseData = {};
client.post(url, args, function(data, response) {
responseData = data;
console.log(responseData);
});
return responseData;
};
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:
Then you can call the function like this: