diff --git a/ln.threading.tests/PromiseTests.cs b/ln.threading.tests/PromiseTests.cs index 70abcb0..9509e29 100644 --- a/ln.threading.tests/PromiseTests.cs +++ b/ln.threading.tests/PromiseTests.cs @@ -34,6 +34,9 @@ namespace ln.threading.tests .Then((value)=>{ return value + 1; }) + .Then((value)=>{ + value ++; + }) .Value ); @@ -57,6 +60,9 @@ namespace ln.threading.tests .Then((value)=>{ return value + 1; }) + .Then((value)=>{ + value ++; + }) .Value ); } diff --git a/ln.threading/Promise.cs b/ln.threading/Promise.cs index 6add56e..568ef66 100644 --- a/ln.threading/Promise.cs +++ b/ln.threading/Promise.cs @@ -151,6 +151,55 @@ namespace ln.threading } } + public Promise Then(Action resolved) => Then(resolved, null); + public Promise Then(Action resolved, Func rejected) + { + lock (this) + { + Promise chainedPromise = new Promise(); + Action resolveAction = ()=>{ + lock (this) + { + switch (State) + { + case PromiseState.PENDING: + throw new Exception("serious bug in Promise.Then(..)"); + case PromiseState.RESOLVED: + try{ + resolved(value); + chainedPromise.Resolve(value); + } catch (Exception e) + { + chainedPromise.Reject(e); + } + break; + case PromiseState.REJECTED: + if (rejected == null) + chainedPromise.Reject(rejectingException); + else + { + try{ + chainedPromise.Resolve(rejected(rejectingException)); + } catch (Exception e) + { + chainedPromise.Reject(e); + } + } + break; + } + } + }; + + if (IsSettled) + DynamicThreadPool.DefaultPool.Enqueue(resolveAction); + else + Settled += (p) => resolveAction(); + + return chainedPromise; + } + } + + public Promise Catch(Func rejectedHandler) { lock (this) diff --git a/ln.threading/ln.threading.csproj b/ln.threading/ln.threading.csproj index f86e0fa..3ed0da1 100644 --- a/ln.threading/ln.threading.csproj +++ b/ln.threading/ln.threading.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 0.2.0 + 0.2.1 Harald Wolff-Thobaben l--n.de