コード例 #1
0
ファイル: Promise.cs プロジェクト: Cushmily/wPromise
        public IPromise Then(Action onResolved, Action <Exception> onRejected, Action <float> onNotified)
        {
            var resultPromise = IsNamed ? Deferred.Create(Name) : Deferred.Create();

            Action resolveHandle = () =>
            {
                try
                {
                    onResolved?.Invoke();

                    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);
        }
コード例 #2
0
ファイル: Promise.cs プロジェクト: Cushmily/wPromise
        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);
        }