I've tried to send a post request to an API and I'm not able to get back the data to my async function , I've tried the next code:
function PostCode(codestring) {
// Build the post string from an object
var post_data = querystring.stringify({
'compilation_level' : 'ADVANCED_OPTIMIZATIONS',
'output_format': 'json',
'output_info': 'compiled_code',
'warning_level' : 'QUIET',
'js_code' : codestring
});
// An object of options to indicate where to post to
var post_options = {
host: 'closure-compiler.appspot.com',
port: '80',
path: '/compile',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
return chunk;
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
And in my async function the call of the function looks like that:
let response = await PostCode(0);
The data is being printed well on that line:
console.log('Response: ' + chunk);
But the return doesn't work at all
how can i return that 'chunk' variable safely to my main async function?