Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Tests/Promise_NonGeneric_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> Create() => Promise.Create<int>((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<ArgumentNullException>(() => Promise.Create(null));
Assert.Throws<ArgumentNullException>(() => Promise.Create<int>(null));
}
}
}
16 changes: 16 additions & 0 deletions src/Promise_NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,22 @@ private Promise(PromiseState initialState)
id = NextId();
}

/// <summary>
/// Create a new promise with no result.
/// </summary>
/// <param name="resolver">resolve action</param>
/// <returns>the constructed promise</returns>
public static IPromise Create(Action<Action, Action<Exception>> resolver)
=> new Promise(resolver ?? throw new ArgumentNullException(nameof(resolver)));

/// <summary>
/// Create a new promise with a result.
/// </summary>
/// <param name="resolver">resolve action</param>
/// <returns>the constructed promise</returns>
public static IPromise<T> Create<T>(Action<Action<T>, Action<Exception>> resolver)
=> new Promise<T>(resolver ?? throw new ArgumentNullException(nameof(resolver)));

/// <summary>
/// Increments the ID counter and gives us the ID for the next promise.
/// </summary>
Expand Down