How to use the EvaluateJavaScriptAsync() method for a JS function that returns a promise?

Marcos Vinicius 0 Reputation points
2024-01-23T12:09:34.37+00:00

I need to access the result of a promise that is returned from a javascript function. To access this function I using the WebView component and the EvaluateJavaScriptAsync() function; But the function always returns empty and too quickly, while in the browser it returns correctly. Does MAUI not support this type of request in JS?

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2024-01-23T17:53:24.7866667+00:00

    the return value from EvaluateJavaScriptAsync() is the promise and not the result of the promise resolve. javascript has no method to turn an async call to sync.

    if you have hooked up javascript calling your .net code, then add to code

    await webview.EvaluateJavaScriptAsync("dotask().then(r => callDotNet(r));

    or you can poll:

    
    await webview.EvaluateJavaScriptAsync("window.result = ''; dotask().then(r => window.result = r"));
    var result = await webview.EvaluateJavaScriptAsync("window.result");
    while(result == "")
    {
       Thread.Sleep(10);
       result = await webview.EvaluateJavaScriptAsync("window.result");
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.