Skip to content
Merged
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
6 changes: 3 additions & 3 deletions Jint.Tests/Runtime/TestClasses/AsyncTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ internal class AsyncTestClass

public async Task AddToStringDelayedAsync(string appendWith)
{
await Task.Delay(1000).ConfigureAwait(false);
await Task.Delay(100).ConfigureAwait(false);

StringToAppend += appendWith;
}

public async Task<string> ReturnDelayedTaskAsync()
{
await Task.Delay(1000).ConfigureAwait(false);
await Task.Delay(100).ConfigureAwait(false);

return TestString;
}
Expand All @@ -36,4 +36,4 @@ public async Task<string> ThrowAfterDelayAsync()

throw new Exception("Task threw exception");
}
}
}
22 changes: 20 additions & 2 deletions Jint/JsValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,14 +645,32 @@ private static JsValue ThrowNotObject(JsValue value)
/// </summary>
/// <param name="value">value to unwrap</param>
/// <returns>inner value if Promise the value itself otherwise</returns>
public static JsValue UnwrapIfPromise(this JsValue value)
public static JsValue UnwrapIfPromise(this JsValue value) => UnwrapIfPromise(value, TimeSpan.FromSeconds(10));

/// <summary>
/// If the value is a Promise
/// 1. If "Fulfilled" returns the value it was fulfilled with
/// 2. If "Rejected" throws "PromiseRejectedException" with the rejection reason
/// 3. If "Pending" throws "InvalidOperationException". Should be called only in "Settled" state
/// Else
/// returns the value intact
/// </summary>
/// <param name="value">value to unwrap</param>
/// <param name="timeout">timeout to wait</param>
/// <returns>inner value if Promise the value itself otherwise</returns>
public static JsValue UnwrapIfPromise(this JsValue value, TimeSpan timeout)
{
if (value is JsPromise promise)
{
var engine = promise.Engine;
var completedEvent = promise.CompletedEvent;

engine.RunAvailableContinuations();
completedEvent.Wait();
if (!completedEvent.Wait(timeout))
{
ExceptionHelper.ThrowPromiseRejectedException($"Timeout of {timeout} reached");
}

switch (promise.State)
{
case PromiseState.Pending:
Expand Down