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?

dahui
  • 115
  • 1
  • 8
  • Possible duplicate of [How can I execute Javascript callback function from C# host application](http://stackoverflow.com/questions/21138740/how-can-i-execute-javascript-callback-function-from-c-sharp-host-application) – Robert Moskal Oct 17 '15 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. – 76484 Oct 18 '15 at 05: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()`. – dahui Oct 18 '15 at 05: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`. – dahui Oct 18 '15 at 05: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`. – 76484 Oct 18 '15 at 05:49
  • @76484 But the ultimate goal is to return `x` to the caller of `B`, or the caller of `A` (if getting rid of `B`). Is that possible? – dahui Oct 18 '15 at 05:56
  • That is a bad goal. If I understand correctly, you want the user's browser to freeze while you synchronously obtain `x`. The point of promises and deferreds is to give you a friendly API for handling async data. Why not use it? – 76484 Oct 18 '15 at 06:02
  • @76484 Yeah I know it's bad. The API I'm using is written in js and async, and my C# application (the caller of `B` or `A`) wants the results from the API. That's why I'm trying to handle the async data and return the final result. – dahui Oct 18 '15 at 06:46
  • I don't understand how C# can be calling this JavaScript. – 76484 Oct 18 '15 at 08:22

1 Answers1

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.

Pavel Durov
  • 1,287
  • 2
  • 13
  • 28