Exemplo n.º 1
0
        public IPromise Catch(Action <Exception> onCatched)
        {
            var resultPromise = IsNamed ? Deferred.Create(Name) : Deferred.Create();

            Action resolveHandle = () =>
            {
                try { resultPromise.Resolve(); }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            Action <Exception> rejectHandle = ex =>
            {
                try
                {
                    onCatched?.Invoke(ex);

                    resultPromise.Reject(ex);
                }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            Action <float> notifyHandle = process =>
            {
                try { resultPromise.Notify(process); }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            AddPromiseHandles(resultPromise, resolveHandle, rejectHandle, notifyHandle);

            return(resultPromise);
        }
Exemplo n.º 2
0
        public IPromise Then(Func <IPromise> onResolved, Action <Exception> onRejected, Action <float> onNotified)
        {
            var resultPromise = IsNamed ? Deferred.Create(Name) : Deferred.Create();

            Action resolveHandle = () =>
            {
                try
                {
                    if (onResolved != null)
                    {
                        onResolved().Then(
                            () => resultPromise.Resolve(),
                            ex => resultPromise.Reject(ex),
                            process => resultPromise.Notify(process)
                            );
                    }
                    else
                    {
                        resultPromise.Resolve();
                    }
                }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            Action <Exception> rejectHandle = ex =>
            {
                try
                {
                    onRejected?.Invoke(ex);

                    resultPromise.Reject(ex);
                }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            Action <float> notifyHandle = process =>
            {
                try
                {
                    onNotified?.Invoke(process);

                    resultPromise.Notify(process);
                }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            AddPromiseHandles(resultPromise, resolveHandle, rejectHandle, notifyHandle);

            return(resultPromise);
        }
Exemplo n.º 3
0
        public IPromise <TPromised> Then <TPromised>(Func <IPromise <TPromised> > onResolved, Action <Exception> onRejected,
                                                     Action <float, TPromised> onNotified)
        {
            var resultPromise = IsNamed ? Deferred.Create <TPromised>(Name) : Deferred.Create <TPromised>();

            Action resolveHandle = () =>
            {
                try
                {
                    onResolved?.Invoke().Then(
                        promised => resultPromise.Resolve(promised),
                        ex => resultPromise.Reject(ex),
                        (process, current) =>
                    {
                        onNotified?.Invoke(process, current);

                        resultPromise.Notify(process, current);
                    });
                }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            Action <Exception> rejectHandle = ex =>
            {
                try
                {
                    onRejected?.Invoke(ex);

                    resultPromise.Reject(ex);
                }
                catch (Exception e) { resultPromise.Reject(e); }
            };

            AddPromiseHandles(resultPromise, resolveHandle, rejectHandle, null);

            return(resultPromise);
        }