0

I'm writing a Javascript function which will be called by a C# application. I can call the function from C#, but have been unable to retrieve the result of the function.

So I have the following structure:

var B = function() {
    var A = function() {
        var dfd = new $.Deferred();
            // do something and then return the value I need
            return dfd.resolve(x);
        ......
        return dfd.promise();
    }
    $.when(A()).
        then(function(x) {
            // I can get the x I want here.
            alert(x);
            // What to do at this point?
        });
}

Since A() used asynchronous methods, I chose to use jQuery.promise() method to ensure I get the final result of A(). Now I want B() to return the value x to the C# application. Is there any good solution to this?

9
  • Possible duplicate of How can I execute Javascript callback function from C# host application Commented Oct 17, 2015 at 23:54
  • What does "return ... to the C# application" mean? C# has exited the picture long before the JavaScript is executed in the client browser. You will need to make a fresh HTTP request to your application, perhaps with the value of x as a query string parameter. Commented Oct 18, 2015 at 5:27
  • @76484 So I'm just invoking the script from C#. My application is synchronous while the API I used isn't, so I want to do something to make function B() appear as synchronous and return the value after everything is done in A(). Commented Oct 18, 2015 at 5:38
  • @RobertMoskal Not really. Mine has almost nothing to do with C#, that's just the background. Mine main goal is to know how to let B() return x. Commented Oct 18, 2015 at 5:45
  • If I understand your question correctly, it is about JS and has nothing to do with C#. If the value of x is obtained asynchronously, then B cannot simply return it synchronously. Rather, B must return dfd.promise and callers of B must use the $.when(B()).then(...); syntax. Of course, that looks like it would amount to getting rid of the B wrapper and using just A. Commented Oct 18, 2015 at 5:49

1 Answer 1

0

I assume that you've called your Javascript function by calling WebView method InvokeScriptAsync with Js function name as a parameter.

The problem here is that your JavaScript function is an asynchronous since it uses promise - so you cannot simply get its return value on C#. The IAsyncOperation ends as the promise object has been returned, not as it has been resolved.

If your javascript function was synchronious:

The return value of the InvokeScriptAsync function is the string result of the script invocation - so if you'll wait for it you'll get the result.

MSDN documentation : https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.invokescriptasync.aspx?f=255&MSPPError=-2147217396

However, you can always invoke webView event by calling windows.external.invoke with string parameter from JavaScript and get it on C# by subscribing to webView ScriptNotify event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.