Exemple #1
0
        public static Promise <PromiseAllResult[], PromiseAllResult[]> ThenAll(IPromise[] promises)
        {
            int resolveCount = 0;

            PromiseAllResult[] promiseAllResult = new PromiseAllResult[promises.Length];

            Deferred <PromiseAllResult[], PromiseAllResult[]> result = new Deferred <PromiseAllResult[], PromiseAllResult[]>();

            Action checkToResolve = () =>
            {
                if (resolveCount != promises.Length)
                {
                    return;
                }
                if (promiseAllResult.All(result1 => result1.Succeded))
                {
                    result.Resolve(promiseAllResult);
                }
                else
                {
                    result.Reject(promiseAllResult);
                }
            };
            int counter = 0;

            foreach (IPromise promise in promises)
            {
                int index = counter;
                InternalPromise <object, object> innerPromise = (InternalPromise <object, object>)promise.GetType().GetField("_internalPromise", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(promise);
                innerPromise.Then(o =>
                {
                    resolveCount++;
                    promiseAllResult[index] = new PromiseAllResult
                    {
                        Result   = o,
                        Succeded = true
                    };
                    checkToResolve();
                }, o =>
                {
                    resolveCount++;
                    promiseAllResult[index] = new PromiseAllResult
                    {
                        Result   = o,
                        Succeded = false
                    };
                    checkToResolve();
                });
                counter++;
            }
            return(result.Promise());
        }
Exemple #2
0
 public ComplitionPromise()
 {
     _internalPromise = new InternalPromise <object, object>();
 }
Exemple #3
0
 public ErrorPromise()
 {
     _internalPromise = new InternalPromise <object, object>();
 }