Monday 9 September 2019

web services - Structure of async callbacks in javascript: Synching the Asynch

I find the seperation of code between when using callbacks makes my code harder to understand and maintain.



How do you handle the problem?




Here are a couple of solutions I have come up with, using, as an exaple, asynch web service calls. Please let me know what you think, and and pros or cons that occur to you.



via closures:



sayHelloWithClosures: function ()
{
//Do something first
// The following call's signature is: ServiceName(SuccessCallback, FailureCallback);
TestBasicWebServices.SL.WebService1.HelloWorld(

function (result)
{
//Do something next
alert(result);
},
function (error)
{
//Do Exception
alert(error._message);
});

}


via recursion:



sayHello: function (result)
{
if (result == undefined)
{
//Do something first

// The following call's signature is: ServiceName(SuccessCallback, FailureCallback);
TestBasicWebServices.SL.WebService1.HelloWorld(this.sayHello, this.sayHello);
}
else if (typeof (result) == "string")
{
//Do something next
alert(result);
}
else
{

//Do Exception
alert(result._message);
}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...