我发现使用回调时的代码分离使我的代码更难以理解和维护。
你如何处理这个问题?
以下是我提出的几个解决方案,例如使用异步 Web 服务调用。请告诉我您的想法以及您认为的优点或缺点。
通过闭包:
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);
});
}
通过递归:
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);
}
}
最佳答案
随着 jquery 的新版本,回调方面的一切都发生了变化。
http://addyosmani.com/blog/jquery-1-7s-callbacks-feature-demystified/
关于javascript - JavaScript 中异步回调的结构 : Synching the Asynch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8211687/