Attention: We are retiring the ASP.NET Community Blogs. Learn more >

A CALLBACK function is always triggered at LAST in a code route !!

A segment of javascript code :

 function getValue() {
          var rel = null;
          CustomService.GetValueFromServer(
          function(result) {
              rel = result;
          }
          );

          return rel;
      }

    var myValue = getValue();

CustomService is a "ScriptService" attributed c# webService class with a functional method "GetValueFromServer" supplying certain string value :

[WebService(Namespace = "http://tempuri.org/"),
WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1),
ScriptService]
public class CustomService : WebService
{
    [WebMethod]
    public string GetValueFromServer()
    {
        return "Yes I have got it.";
    }
}
        

Then , guess what will happen to "myValue" ? Will it be "Yes I have got it" ?? No , its value will be null after assignation.

The reason is revealed in article subject. CALLBACK is always occuring at the very end.

If you are not quit familar with client invocation of webservice please reference the official link :

Calling Web Services from Client Script in ASP.NET AJAX

Now let me explain the short route.

"var myValue = getValue()" invokes getValue() method , where following steps execute subsequently :

  1. "rel" is initialized with null
  2. CustomService.GetValueFromServer() is called. But no js code excutes at this time at client! Note the function() is just a CALLBACK on a successful condition! As subject reveals , it will not be ruded in at this time , meaning "result" is not passed to rel.
  3. "rel" , a variable not passed with any value and staying null , is returned.
  4. Consequently , "myValue" only get a null value.
  5. Eventually , the "onSucceed" callback function is fired ! Whatever happens in it , it loses the chance to provide client an expected value.

No Comments