From aefe94f23f492f54dd89713a12a468a07e00ee56 Mon Sep 17 00:00:00 2001 From: Niek Jannink Date: Fri, 18 Oct 2019 10:40:25 -0500 Subject: [PATCH 1/2] Factory methods for Promise creation --- src/Promise_NonGeneric.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Promise_NonGeneric.cs b/src/Promise_NonGeneric.cs index b10c6b6..5b82f8b 100644 --- a/src/Promise_NonGeneric.cs +++ b/src/Promise_NonGeneric.cs @@ -375,6 +375,22 @@ private Promise(PromiseState initialState) id = NextId(); } + /// + /// Create a new promise with no result. + /// + /// resolve action + /// the constructed promise + public static IPromise Create(Action> resolver) + => new Promise(resolver ?? throw new ArgumentNullException(nameof(resolver))); + + /// + /// Create a new promise with a result. + /// + /// resolve action + /// the constructed promise + public static IPromise Create(Action, Action> resolver) + => new Promise(resolver ?? throw new ArgumentNullException(nameof(resolver))); + /// /// Increments the ID counter and gives us the ID for the next promise. /// From 2b3a9b570b679079f06801076e984a65eccae9e2 Mon Sep 17 00:00:00 2001 From: Niek Jannink Date: Wed, 23 Oct 2019 10:54:32 -0500 Subject: [PATCH 2/2] Added some tests for the factory methods --- Tests/Promise_NonGeneric_Tests.cs | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Tests/Promise_NonGeneric_Tests.cs b/Tests/Promise_NonGeneric_Tests.cs index 7cd07e9..759cb88 100644 --- a/Tests/Promise_NonGeneric_Tests.cs +++ b/Tests/Promise_NonGeneric_Tests.cs @@ -1295,5 +1295,48 @@ public void can_chain_promise_after_ContinueWith() Assert.Equal(2, callback); } + + [Fact] + public void can_create_promise() + { + int callback = 0; + + IPromise Create() => Promise.Create((resolve, reject) => + { + resolve(); + ++callback; + }); + + var promise = new Promise(); + promise.Then(Create); + promise.Resolve(); + + Assert.Equal(1, callback); + } + + [Fact] + public void can_create_oft_promise() + { + int callback = 0; + + IPromise Create() => Promise.Create((resolve, reject) => + { + resolve(1); + ++callback; + }); + + var promise = new Promise(); + promise.Then(Create); + promise.Resolve(); + + Assert.Equal(1, callback); + } + + [Fact] + public void can_create_null_throws() + { + Assert.Throws(() => Promise.Create(null)); + Assert.Throws(() => Promise.Create(null)); + } } }