Haxe - How do you implement async await for Flash Targets

Viewed 693

I currently have this code:

package sage.sys;

import com.dongxiguo.continuation.Async;

#if flash
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
#end

class FileSystem implements Async
{
    public static function fetchText(url:String, callback:String->Void) : Void
    {
        var urlLoader = new URLLoader();
        var onLoaderError = function(e : Event) : Void {
        callback(e.type);
    };

    urlLoader.addEventListener(Event.COMPLETE, function(_) : Void {
        callback(Std.string(urlLoader.data));
    });

    urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoaderError);
    urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoaderError);

    try {
        urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
        urlLoader.load(new URLRequest(url));
    }
    catch (e : Dynamic)
    {
        callback(Std.string(e));
    }
}

@async
public static function fetch(url:String):String
{
    var result = @await fetchText(url);
    return result;
}
}

When I try to run it, it doesn't wait at the await, and returns from the function. How can I make the @await actually enforce itself and and stop running outside the function till the value is resolved from the async call?

Lib used: https://github.com/proletariatgames/haxe-continuation

2 Answers
Related