jdonaldson / promhx

A promise and functional reactive programming library for Haxe
MIT License
145 stars 24 forks source link

Using Promise in sync way + error #79

Closed andyli closed 8 years ago

andyli commented 8 years ago

I'm writing a function that may perform an HTTP download, that may be sync/async depended on target. Here is the part that illustrate the sync logic:

import promhx.*;

class Test {
    static function download():Promise<Bytes> {
        var d = new Deferred();
        var p = d.promise();

        //assume there is an error
        d.throwError("HTTP Error");

        return p;
    }
    static function main():Void {
        var d = download()
            .then(function(b){
                trace("downloaded");
            })
            .catchError(function(err){ // this should be called but it is not
                trace("catched error: " + err);
            });
    }
}

The problem is that, when the error occurs, the Promise has not been returned to the call-site yet. It means the error handler has not yet been attached, thus the Promise will throw the error immediately.

Is there a workaround or am I using it wrongly?

hexonaut commented 8 years ago

Which language? Languages with an event loop will defer the error handling for a future event. Otherwise you need to set up how the event loop is called and your own threading model.

andyli commented 8 years ago

I see.. I'm targeting Neko. Look like Promise is not very well-suited to targets without an event loop.