Example #1
0
        public static IPromise <IEnumerable <PromisedT> > All(IEnumerable <IPromise <PromisedT> > promises)
        {
            IPromise <PromisedT>[] array = promises.ToArray();
            if (array.Length == 0)
            {
                return(Promise <IEnumerable <PromisedT> > .Resolved(EnumerableExt.Empty <PromisedT>()));
            }
            int remainingCount = array.Length;

            PromisedT[] results = new PromisedT[remainingCount];
            Promise <IEnumerable <PromisedT> > resultPromise = new Promise <IEnumerable <PromisedT> >();

            resultPromise.WithName("All");
            array.Each(delegate(IPromise <PromisedT> promise, int index)
            {
                promise.Catch(delegate(Exception ex)
                {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        resultPromise.Reject(ex);
                    }
                }).Then(delegate(PromisedT result)
                {
                    results[index] = result;
                    remainingCount--;
                    if (remainingCount <= 0)
                    {
                        resultPromise.Resolve(results);
                    }
                }).Done();
            });
            return(resultPromise);
        }
Example #2
0
        public void combined_promise_is_resolved_if_there_are_no_promises()
        {
            var all = Promise.All(EnumerableExt.Empty <IPromise>());

            var completed = 0;

            all.Then(() => ++ completed);
        }
Example #3
0
        /// <summary>
        /// Returns a promise that resolves when all of the promises in the enumerable argument have resolved.
        /// Returns a promise of a collection of the resolved results.
        /// </summary>
        public static IPromise <IEnumerable <PromisedT> > All(IEnumerable <IPromise <PromisedT> > promises)
        {
            var promisesArray = promises.ToArray();

            if (promisesArray.Length == 0)
            {
                return(Promise <IEnumerable <PromisedT> > .Resolved(EnumerableExt.Empty <PromisedT>()));
            }

            var remainingCount = promisesArray.Length;
            var results        = new PromisedT[remainingCount];
            var progress       = new float[remainingCount];
            /// ADDED: Progress ripped from 3.0
            var resultPromise = new Promise <IEnumerable <PromisedT> >();

            resultPromise.WithName("All");

            promisesArray.Each((promise, index) =>
            {
                promise
                /// ADDED: Progress ripped from 3.0
                .Progress(v =>
                {
                    progress[index] = v;
                    resultPromise.ReportProgress(progress.Average());
                })
                .Catch(ex =>
                {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        // If a promise errorred and the result promise is still pending, reject it.
                        resultPromise.Reject(ex);
                    }
                })
                .Then(result =>
                {
                    /// ADDED: Progress ripped from 3.0
                    progress[index] = 1f;
                    results[index]  = result;

                    --remainingCount;
                    if (remainingCount <= 0)
                    {
                        // This will never happen if any of the promises errorred.
                        resultPromise.Resolve(results);
                    }
                })
                .Done();
            });

            return(resultPromise);
        }
Example #4
0
        public void combined_promise_is_resolved_if_there_are_no_promises()
        {
            var all = Promise <int> .All(EnumerableExt.Empty <IPromise <int> >());

            var completed = 0;

            all.Then(v =>
            {
                ++completed;

                Assert.Empty(v);
            });

            Assert.Equal(1, completed);
        }