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?
xas a query string parameter.B()appear as synchronous and return the value after everything is done inA().B()returnx.xis obtained asynchronously, thenBcannot simply return it synchronously. Rather,Bmust returndfd.promiseand callers ofBmust use the$.when(B()).then(...);syntax. Of course, that looks like it would amount to getting rid of theBwrapper and using justA.