javascript - JavaScript 中异步回调的结构 : Synching the Asynch

标签 javascript web-services asynchronous

我发现使用回调时的代码分离使我的代码更难以理解和维护。

你如何处理这个问题?

以下是我提出的几个解决方案,例如使用异步 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/

相关文章:

c++ - std::async 与共享指针模板化成员函数

c# - 通过 IPC 传递任务<T>

javascript - 如何仅在提交时显示 jQuery 验证错误容器

asynchronous - 异步FromBeginEnd接口(interface)

javascript - 上一篇状态不一致

javascript - 使用 PHP 获取 Web 服务数据

c# - 当 Web 服务返回结果时,您如何检测连接何时中断?

c# - 我如何解决这个错误, "error while trying to deserialize parameter"

javascript - Mongoose - 如果存在,则在 Subdoc 上增加字段,否则创建新的

页面最小化/离开后的Javascript setInterval "catches up"