Ejemplo n.º 1
0
        public IPromise <U> Then <U>(Func <T, IPromise <U> > onResolved, Func <Exception, IPromise <U> > onRejected)
        {
            if (CurrentState == PromiseState.Resolved)
            {
                return(onResolved(resolveValue));
            }
            else if (CurrentState == PromiseState.Rejected)
            {
                return(Promise.Rejected <U>(rejectException));
            }

            var resultPromise = new Promise <U>();

            if (onResolved != null)
            {
                void resolveAction(T _)
                {
                    var promise = onResolved(resolveValue);

                    promise.Then(resultPromise.Resolve);
                    promise.Catch(resultPromise.Reject);
                }

                resolveActions.Add(new ResolveAction <T> {
                    action = resolveAction, rejectable = resultPromise
                });
            }

            Action <Exception> rejectAction;

            if (onRejected != null)
            {
                rejectAction = (e) =>
                {
                    var promise = onRejected(e);
                    promise.Then(resultPromise.Resolve);
                    promise.Catch(resultPromise.Reject);
                };
            }
            else
            {
                rejectAction = resultPromise.Reject;
            }

            rejectActions.Add(new RejectAction {
                action = rejectAction, rejectable = resultPromise
            });

            return(resultPromise);
        }
Ejemplo n.º 2
0
        public IPromise Then(Func <IPromise> onResolved, Action <Exception> onRejected)
        {
            if (CurrentState == PromiseState.Resolved)
            {
                return(onResolved());
            }
            else if (CurrentState == PromiseState.Rejected)
            {
                return(Promise.Rejected(rejectException));
            }

            var resultPromise = new Promise();

            if (onResolved != null)
            {
                void resolveAction()
                {
                    var promise = onResolved();

                    promise.Then(resultPromise.Resolve);
                    promise.Catch(resultPromise.Reject);
                }

                resolveActions.Add(new ResolveAction {
                    action = resolveAction, rejectable = resultPromise
                });
            }

            Action <Exception> rejectAction;

            if (onRejected != null)
            {
                rejectAction = (e) =>
                {
                    onRejected(e);
                    resultPromise.Reject(e);
                };
            }
            else
            {
                rejectAction = resultPromise.Reject;
            }

            rejectActions.Add(new RejectAction {
                action = rejectAction, rejectable = resultPromise
            });

            return(resultPromise);
        }