1

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?

1 Answer 1

1

You need to return a Promise when you want to use await and the Promise needs to resolve with your response.

function PostCode(codestring) {
  return new Promise((resolve, reject) => {
    // 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)
      }
    };

    //Store chunks
    var response = "";
    // Set up the request
    var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function(chunk) {
        response += chunk;
        console.log('Response: ' + chunk);
      });
      res.on('end', function() {
        //resolve on end event
        resolve(response);
      });
      res.on('error', function(error) {
        //reject on error event
        reject(error);
      });
    });

    // post the data
    post_req.write(post_data);
    post_req.end();
  });
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.