Пример #1
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 All(IEnumerable <IPromise> promises)
        {
            var promisesArray = promises.ToArray();

            if (promisesArray.Length == 0)
            {
                return(Promise.Resolved());
            }

            var remainingCount = promisesArray.Length;
            var resultPromise  = new Promise();

            promisesArray.Each((promise, index) =>
            {
                promise
                .Catch(ex =>
                {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        // If a promise errorred and the result promise is still pending, reject it.
                        resultPromise.Reject(ex);
                    }
                })
                .Then(() =>
                {
                    --remainingCount;
                    if (remainingCount <= 0)
                    {
                        // This will never happen if any of the promises errorred.
                        resultPromise.Resolve();
                    }
                })
                .Done();
            });

            return(resultPromise);
        }
Пример #2
0
        /// <summary>
        /// Chains another asynchronous operation.
        /// Converts to a promisse that has no result.
        /// </summary>
        public IPromise Then(Func <PromisedT, IPromise> chain)
        {
            Argument.NotNull(() => chain);

            var resultPromise = new Promise();

            this.Catch(e => resultPromise.Reject(e))
            .Then(v =>
            {
                try
                {
                    chain(v)
                    .Catch(e => resultPromise.Reject(e))
                    .Then(() => resultPromise.Resolve())
                    .Done();
                }
                catch (Exception ex)
                {
                    resultPromise.Reject(ex);
                }
            });

            return(resultPromise);
        }
Пример #3
0
        /// <summary>
        /// Chains another asynchronous operation.
        /// May convert to a promise that yields a value.
        /// </summary>
        public IPromise <ConvertedT> Then <ConvertedT>(Func <IPromise <ConvertedT> > chain)
        {
            Argument.NotNull(() => chain);

            var resultPromise = new Promise <ConvertedT>();

            this.Catch(e => resultPromise.Reject(e))
            .Then(() =>
            {
                try
                {
                    chain()
                    .Catch(e => resultPromise.Reject(e))
                    .Then(chainedValue => resultPromise.Resolve(chainedValue))
                    .Done();
                }
                catch (Exception ex)
                {
                    resultPromise.Reject(ex);
                }
            });

            return(resultPromise);
        }