jdonaldson / promhx

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

Issues with Promise.when #22

Closed wighawag closed 10 years ago

wighawag commented 10 years ago

Hi,

I am getting back into haxe and I' d like to use promhx.

After getting hand on some old code that were working with some version of promhx (I think), it does not seem to work anymore.

The following code fails on the Promise.when. I tried using Promise.whenAll but it also fails saying among other errors :

assets : Array<loadBatch.T> -> Void should be Array<Dynamic> -> Void

Note : AssetLoader return a Promise of type T

import promhx.Promise;
import wighawag.asset.load.AssetLoader;

class BatchLoader{

    inline static public function loadBatch<T : Asset>(assetLoader : AssetLoader<T>, ids : Array<AssetId>, ?paths : Array<String> = null) : Promise<Batch<T>>{
        if(paths == null){
            paths = ids;
        }

        var batchPromise = new Promise();

        function onError (error):Void{
            batchPromise.reject(error);
        }

        var assetPromises = new Array<Promise<T>>();
        for (i in 0...ids.length){
            var p = assetLoader.load(ids[i], paths[i]);
            p.error(onError); //needed as promhx.Promise.when doe snot handle error on each indivudal promisess (https://github.com/jdonaldson/promhx/issues/10)
            assetPromises.push(p);
        }

        Promise.when(assetPromises).then(function(assets : Array<T>): Void{
            batchPromise.resolve(new Batch<T>(assets));
        }).error(onError);

        return batchPromise;
    }

any ideas?

jdonaldson commented 10 years ago

The short answer is that whenAll should work here, if it's not that's my problem. Once that is working you'll get the error handling, etc. for free.

I'm confused with:

Promise.when(assetPromises)...

The compiler should be erroring there, telling you that you can't pass an array of promises, only one or more promises as arguments (variadic).

Are you on the latest promhx? It looks like you're using some older signatures. E.g., Promise.error was replaced by Promise.catchError. (and Promise.errorThen lets you recover from errors gracefully). The latest version might fix your Promise.whenAll problems.

wighawag commented 10 years ago

Sorry I was using Promise.whenAll(assetPromises) to get the error I mentioned.

When using `Promise.when(assetPromises)I getArray<promhx.Promise> should be promhx.Promise``

I am using promhx 0.2.9 and I can only see "error" and no "catchError" . when you say latest version I suppose you are talking about the one on github? I will try this.

Thanks

wighawag commented 10 years ago

I just tried with the version from git and it works without problem, thanks