I cannot create a global variable in a function
function getFabrics(){
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "af_general.php?action=fabrics", true);
    xhttp.send();
    xhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            fabrics_json=JSON.parse(this.responseText);
            console.log(fabrics_json);
        }
    };
}
and use it in another:
function addrow(){
    getFabrics();
    var table=document.getElementById('stoixeia');
    var rows = table.getElementsByTagName("tr").length;
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(-1);
    var rowno=rows-1;
    var pcs_ent=document.createElement('input');
    setAttributes(pcs_ent,{'type':'number','id':'pcs.'+rowno,'name':'pcs','style':'width: 4em;text-align:right','min':'0','max':'99','autocomplete':'off'});
    cell1.innerHTML='#'+rowno;
    cell1.appendChild(pcs_ent);
    var cell2 = row.insertCell(-1);
    var fabric_ent=document.createElement('select');
    console.log(fabrics_json);//returns undefined
    for (f in fabrics_json){
        option = document.createElement('option');
        option.value=fabrics_json[f][0];
        option.text=fabrics_json[f][1];
        fabric_ent.add(option);
    }
    //and goes on...
}
I have read all possible questions in stackoverflow but I can't find a way to get the results. fabrics_json remains undefined either if I declare it in the beginning of the script or not.
 
     
    