Problem with my loader to bitmapdata converter

        public static function loaderToBitmapData(str:String):BitmapData {
            var loader:Loader = new Loader();


            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, tobitmapdata);
            loader.load(new URLRequest(str));
            
            
             function tobitmapdata(e:Event):BitmapData {


                var decodedBitmapData:BitmapData=Bitmap(e.target.content).bitmapData;


                return decodedBitmapData;
            }
            
            return tobitmapdata()
        }

The problem was I could not get the first return to the last. If I didn’t had the tobitmapdata event at all to begin with. The entire function would return a null:

        public static function loaderToBitmapData(str:String):BitmapData {
            var loader:URLLoader = new URLLoader();


            //loader.contentLoaderInfo.addEventListener(Event.COMPLETE, tobitmapdata);
            loader.load(new URLRequest(str));
            
            
            // function tobitmapdata(e:Event):BitmapData {


                //var decodedBitmapData:BitmapData=Bitmap(e.target.content).bitmapData;


                //return decodedBitmapData;
            //}
            
            return Bitmap(loader.data).bitmapData
        }