我发现使用回调之间的代码分离使得我的代码难以理解和维护。javascript中的异步回调的结构:同步异步
你是如何处理这个问题的?
下面是我提出的几个解决方案,使用它作为一个例外的异步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);
}
}
谢谢!这真的很酷。不幸的是,我不明白它将如何帮助解决上述可读性问题...... – bnieland