Javascript returns undefined but prints value [duplicate]

  javascript, node.js, xmlhttprequest

Hello my following code sends a post request to a server and returns data. Inside my function if I use console.log() it will show the value. but If I return it, it shows no value.

function info() {

 var xhr = new XMLHttpRequest();"POST", url);
 xhr.setRequestHeader("Content-Type", "application/json");
 xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
     //   console.log(xhr.status);
     //parse info
     let result = xhr.responseText.split('"info":').pop().split(",")[0];
     console.log(result) // shows the value
     return(result) //does not return value



I Did also noticed that the printout comes after the return. almost like the function does not wait to finish

Source: Ask Javascript Questions