I know this issue have been discussed well on this topic, but I can't seem to find how to apply this to my function. I keep getting undefined variables logs.
function requestLayersFromGeoserver() {
    var parser = new ol.format.WMSCapabilities();
    $.ajax({
        url: "http://localhost:8080/geoserver/ows?service=wms&version=1.1.0&request=GetCapabilities", 
        success: function(response) {
            result = parser.read(response);
            var layersArray = result.Capability.Layer.Layer;
            layersNameArray = [];
            for(i=0;i<layersArray.length;i++){
                layersNameArray.push(layersArray[i].Name)
            }
        }
    }); 
    return layersNameArray
}
How can I fix that so that I get the layersNameArray variable when I call the function ?
 
    