diff --git a/Jint.Tests/Runtime/TestClasses/AsyncTestClass.cs b/Jint.Tests/Runtime/TestClasses/AsyncTestClass.cs index d294a9d6e8..8e83ddd610 100644 --- a/Jint.Tests/Runtime/TestClasses/AsyncTestClass.cs +++ b/Jint.Tests/Runtime/TestClasses/AsyncTestClass.cs @@ -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 ReturnDelayedTaskAsync() { - await Task.Delay(1000).ConfigureAwait(false); + await Task.Delay(100).ConfigureAwait(false); return TestString; } @@ -36,4 +36,4 @@ public async Task ThrowAfterDelayAsync() throw new Exception("Task threw exception"); } -} \ No newline at end of file +} diff --git a/Jint/JsValueExtensions.cs b/Jint/JsValueExtensions.cs index 2d1f3d580d..3576e8e8d6 100644 --- a/Jint/JsValueExtensions.cs +++ b/Jint/JsValueExtensions.cs @@ -645,14 +645,32 @@ private static JsValue ThrowNotObject(JsValue value) /// /// value to unwrap /// inner value if Promise the value itself otherwise - public static JsValue UnwrapIfPromise(this JsValue value) + public static JsValue UnwrapIfPromise(this JsValue value) => UnwrapIfPromise(value, TimeSpan.FromSeconds(10)); + + /// + /// 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 + /// + /// value to unwrap + /// timeout to wait + /// inner value if Promise the value itself otherwise + 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: