diff Implab/Safe.cs @ 66:790e8a997d30

Refactoring
author cin
date Thu, 14 Aug 2014 18:08:09 +0400
parents c0bf853aa04f
children efcb076407a7
line wrap: on
line diff
--- a/Implab/Safe.cs	Mon Jun 30 13:55:22 2014 +0400
+++ b/Implab/Safe.cs	Thu Aug 14 18:08:09 2014 +0400
@@ -3,6 +3,7 @@
 using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
+using System.Diagnostics;
 
 namespace Implab
 {
@@ -36,5 +37,30 @@
             if (disp != null)
                 disp.Dispose();
         }
+
+        [DebuggerStepThrough]
+        public static IPromise<T> GuargPromise<T>(Func<T> action) {
+            ArgumentNotNull(action, "action");
+
+            var p = new Promise<T>();
+            try {
+                p.Resolve(action());
+            } catch (Exception err) {
+                p.Reject(err);
+            }
+
+            return p;
+        }
+
+        [DebuggerStepThrough]
+        public static IPromise<T> GuardPromise<T>(Func<IPromise<T>> action) {
+            ArgumentNotNull(action, "action");
+
+            try {
+                return action();
+            } catch (Exception err) {
+                return Promise<T>.ExceptionToPromise(err);
+            }
+        }
     }
 }