Mercurial > pub > ImplabNet
diff Implab/Promise.cs @ 260:547a2fc0d93e v3 v3.0.6
minor fixes
author | cin |
---|---|
date | Fri, 13 Apr 2018 19:14:59 +0300 |
parents | d82909310094 |
children |
line wrap: on
line diff
--- a/Implab/Promise.cs Fri Apr 13 03:57:39 2018 +0300 +++ b/Implab/Promise.cs Fri Apr 13 19:14:59 2018 +0300 @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using Implab.Parallels; @@ -152,31 +153,44 @@ } public static IPromise Create(PromiseExecutor executor) { - Safe.ArgumentNotNull(executor, nameof(executor)); + return Create(executor, CancellationToken.None); + } - var p = new Promise(); - var d = new Deferred(p, DefaultDispatcher); - + public static IPromise Create(PromiseExecutor executor, CancellationToken ct) { + Safe.ArgumentNotNull(executor, nameof(executor)); + if (!ct.CanBeCanceled) + return Create(executor); + + var d = new Deferred(); + + ct.Register(d.Cancel); + try { - executor(d); - } catch (Exception e) { + if (!ct.IsCancellationRequested) + executor(d); + } catch(Exception e) { d.Reject(e); } - return d.Promise; } public static IPromise<T> Create<T>(PromiseExecutor<T> executor) { + return Create(executor, CancellationToken.None); + } + + public static IPromise<T> Create<T>(PromiseExecutor<T> executor, CancellationToken ct) { Safe.ArgumentNotNull(executor, nameof(executor)); var d = new Deferred<T>(); - + + ct.Register(d.Cancel); + try { - executor(d); - } catch (Exception e) { + if (!ct.IsCancellationRequested) + executor(d); + } catch(Exception e) { d.Reject(e); } - return d.Promise; }