I was trying to return a string from a function to be available as a global variable using the following code, but I didn't realize what's wrong.
Is there a way to get the JSON result outside the function?
var out = '';
function getAgregData() {
  $.getJSON('http://172.16.13.247:5002/webservice//?module=1', function(data) {
    const sourceObject = data;
    const sourceObjectKeys = Object.keys(sourceObject);
    //console.log('AgrKeys: ',sourceObjectKeys);
    let agr = {};
    sourceObjectKeys.forEach((key, i) => {
      agr[key] = sourceObject[key]['agreg'];
    });
    let out = JSON.stringify(agr);
    console.log('Output inside function:', out); // <=== Here I got out variable printed on Console.
    return out;
  });
}
var test = getAgregData();
console.log('Output outside function: ', test); // <=== Here I didn't get out inside test variable printed on Console.
 
    