示例#1
0
        /// <summary>
        /// Add a resolved callback, a rejected callback and a progress callback.
        /// The resolved callback chains a non-value promise.
        /// </summary>
        public IPromise Then(Func <IPromise> onResolved, Action <Exception> onRejected, Action <float> onProgress)
        {
            var resultPromise = new Promise();

            resultPromise.WithName(Name);

            Action resolveHandler = () => {
                if (onResolved != null)
                {
                    onResolved()
                    .Progress(progress => resultPromise.ReportProgress(progress))
                    .Then(
                        () => resultPromise.Resolve(),
                        ex => resultPromise.Reject(ex)
                        );
                }
                else
                {
                    resultPromise.Resolve();
                }
            };

            Action <Exception> rejectHandler = ex => {
                if (onRejected != null)
                {
                    onRejected(ex);
                }

                resultPromise.Reject(ex);
            };

            ActionHandlers(resultPromise, resolveHandler, rejectHandler);
            if (onProgress != null)
            {
                ProgressHandlers(this, onProgress);
            }

            return(resultPromise);
        }
示例#2
0
        /// <summary>
        /// Handle errors for the promise.
        /// </summary>
        public IPromise <PromisedT> Catch(Func <Exception, PromisedT> onRejected)
        {
            var resultPromise = new Promise <PromisedT>();

            resultPromise.WithName(Name);

            Action <PromisedT> resolveHandler = v => resultPromise.Resolve(v);

            Action <Exception> rejectHandler = ex => {
                try {
                    resultPromise.Resolve(onRejected(ex));
                }
                catch (Exception cbEx) {
                    resultPromise.Reject(cbEx);
                }
            };

            ActionHandlers(resultPromise, resolveHandler, rejectHandler);
            ProgressHandlers(resultPromise, v => resultPromise.ReportProgress(v));

            return(resultPromise);
        }
示例#3
0
        /// <summary>
        /// Returns a promise that resolves when the first of the promises in the enumerable argument have resolved.
        /// Returns the value from the first promise that has resolved.
        /// </summary>
        public static IPromise Race(IEnumerable <IPromise> promises)
        {
            var promisesArray = promises.ToArray();

            if (promisesArray.Length == 0)
            {
                throw new InvalidOperationException("At least 1 input promise must be provided for Race");
            }

            var resultPromise = new Promise();

            resultPromise.WithName("Race");

            var progress = new float[promisesArray.Length];

            promisesArray.Each((promise, index) => {
                promise
                .Progress(v => {
                    progress[index] = v;
                    resultPromise.ReportProgress(progress.Max());
                })
                .Catch(ex => {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        // If a promise errorred and the result promise is still pending, reject it.
                        resultPromise.Reject(ex);
                    }
                })
                .Then(() => {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        resultPromise.Resolve();
                    }
                })
                .Done();
            });

            return(resultPromise);
        }
示例#4
0
        public IPromise <PromisedT> Finally(Action onComplete)
        {
            var promise = new Promise <PromisedT>();

            promise.WithName(Name);

            this.Then(x => promise.Resolve(x));
            this.Catch(e => {
                try {
                    onComplete();
                    promise.Reject(e);
                }
                catch (Exception ne) {
                    promise.Reject(ne);
                }
            });

            return(promise.Then(v => {
                onComplete();
                return v;
            }));
        }