I'm using DotNetBrowser as a sort of headless Javascript engine within a .Net app. It seems DotNetBrowser has no support for Promises, or I am not using invoking them properly.
In JS:
window.fooAsyncMethod = () => {
return new Promise((resolve, reject) => {
resolve('async value to resolve');
});
}
In C#:
var result = await browser.MainFrame.ExecuteJavascript("await window.fooAsyncMethod()")
In this case, the result is always null. I've tried async/await as well, and always receive null:
window.fooAsyncMethod = async () => {
return await someOtherAsyncMethod()
}
Does DotNetBrowser support running asynchronous javascript or not? And if so, how can I achieve awaiting the result of an asynchronous operation and resolving that value within C#?
Looking over the JS-.NET bridge examples it appears that it does support promises however they are a bit more involved to use.
If you implement a type of promise object on your C# side as such:
public static class JsObjectExtensions
{
#region Methods
public static JsPromise AsPromise(this IJsObject jsObject) => JsPromise.AsPromise(jsObject);
#endregion
}
public sealed class JsPromise
{
private readonly IJsObject jsObject;
#region Constructors
private JsPromise(IJsObject jsObject)
{
this.jsObject = jsObject;
}
#endregion
public static JsPromise AsPromise(IJsObject jsObject) => !IsPromise(jsObject) ? null : new JsPromise(jsObject);
public JsPromise Catch(Func<object, object> onRejected)
{
IJsObject newPromise = jsObject.Invoke("catch", onRejected) as IJsObject;
return new JsPromise(newPromise);
}
public Task<Result> ResolveAsync()
{
TaskCompletionSource<Result> promiseTcs = new TaskCompletionSource<Result>();
Then(obj => { promiseTcs.TrySetResult(Fulfilled(obj)); },
obj => { promiseTcs.TrySetResult(Rejected(obj)); });
promiseTcs.Task.ConfigureAwait(false);
return promiseTcs.Task;
}
public void Then(Action<object> onFulfilled, Action<object> onRejected = null)
{
jsObject.Invoke("then", onFulfilled, onRejected);
}
public JsPromise Then(Func<object, object> onFulfilled, Func<object, object> onRejected = null)
{
IJsObject newPromise = jsObject.Invoke("then", onFulfilled, onRejected) as IJsObject;
return new JsPromise(newPromise);
}
private Result Fulfilled(object o) => new Result(ResultState.Fulfilled, o);
private static bool IsPromise(IJsObject jsObject)
{
if (jsObject == null || jsObject.IsDisposed)
{
return false;
}
IJsObject promisePrototype = jsObject.Frame.ExecuteJavaScript<IJsObject>("Promise.prototype").Result;
return promisePrototype.Invoke<bool>("isPrototypeOf", jsObject);
}
private Result Rejected(object o) => new Result(ResultState.Rejected, o);
public enum ResultState
{
Fulfilled,
Rejected
}
public class Result
{
public object Data { get; }
public ResultState State { get; }
internal Result(ResultState state, object data)
{
State = state;
Data = data;
}
}
}
You will then be able to call the promise like this:
IJsObject window = browser.MainFrame.ExecuteJavaScript<IJsObject>("window").Result;
//Prepare promise handlers
Action<object> promiseResolvedHandler = o => Console.WriteLine("Success: " + o);
Action<object> promiseRejectedHandler = o => Console.Error.WriteLine("Error: " + o);
//Create a promise that is fulfilled
Console.WriteLine("Create a promise that is fulfilled...");
IJsObject promise1 = window.Invoke<IJsObject>("CreatePromise", true);
//Append fulfillment and rejection handlers to the promise
promise1.Invoke("then", promiseResolvedHandler, promiseRejectedHandler);
//Create a promise that is rejected
Console.WriteLine("Create a promise that is rejected...");
IJsObject promise2 = window.Invoke<IJsObject>("CreatePromise", false);
//Append fulfillment and rejection handlers to the promise
promise2.Invoke("then", promiseResolvedHandler, promiseRejectedHandler);
CreatePromiseAsync(window).Wait();
Please note that all the above code was taken directly from their Promises Example.