使用deferred和pipe的链回调 [英] chain callback using deferred and pipe

查看:79
本文介绍了使用deferred和pipe的链回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用deferred将回调与管道链接起来.它似乎工作正常,但在callback2中,它从callback1带给我数据.代码如下:

I am struggling to chain the callback with pipes using deferred. It seems to work just fine but in the callback2, it brings me data from callback1. Here is how the code looks like:

var getCall1 = function() {
    return $.ajax(url, {
        type: "GET",
        data: { },
        contentType: "application/json",
        dataType: "json"
    });
}


var getCall2 = function () {
    return $.ajax(url, {
        type: "GET",
        data: {},
        contentType: "application/json",
        dataType: "json"
    });
}

var callback1 = function (data)
            {
                alert('call 1 completed');
            };

var callback2 = function (data)
            {
                alert('call 2 completed');
            };

$.when(getCall1()).done(callBack1).pipe(getCall2()).done(callBack2);

我在这里想念什么?

-编辑-

如果我将它们分成两个延迟的呼叫,它可以工作,但是链接和管道的用途是什么?

If i break them in to two deferred calls, it works but then whats the use of chaining and pipe?

$.when(getCall1()).done(callBack1);
$.when(getCall2()).done(callBack2);

-正确的方法-

var getCall1 = function() {
    return $.ajax(url, {
        type: "GET",
        data: { },
        contentType: "application/json",
        dataType: "json"
    });
}.pipe(function (d){return d});


var getCall2 = function () {
    return $.ajax(url, {
        type: "GET",
        data: {},
        contentType: "application/json",
        dataType: "json"
    });
}.pipe(function (d){return d});

var callback1 = function (data)
            {
                alert('call 1 completed');
            };

var callback2 = function (data)
            {
                alert('call 2 completed');
            };

        $.when(getCall1, getCall2).done(function(d1, d2){
            callback1(d1);
            callback2(d2);

        });

推荐答案

我在您的代码中了解的是,只有在getCall1成功的情况下,您才想调用getCall2.

What i understand in your code is you want to call getCall2 only if getCall1 successed.

所以您的代码似乎不错.我在jsFiddle中尝试过(有一个更简单的示例),它对我来说非常有用:

So your code seems to be good. I tried it in jsFiddle (with a more simple example) and it works great for me :

http://jsfiddle.net/PG3aN/

也许这是一个愚蠢的问题,但是您确定getCall1和getCall2不应只是从服务器返回相同的结果吗?那可以解释为什么您在回调1和2中得到相同的数据.

Maybe it's a stupid question but are you sure that getCall1 and getCall2 should not simply return the same result from server ? That could explain why you get same data in callbacks 1 and 2.

其他问题,在您的上一个代码示例中,您编写

Other question, in your last code sample, you write

var getCall1 = function() {
return $.ajax(url, {
        type: "GET",
        data: { },
        contentType: "application/json",
        dataType: "json"
    });
}.pipe(function (d){return d});

最后一个管道会改变代码行为吗?

Does the last pipe change something on your code behaviour ?

关于最后一个代码示例的最后一句话,预期结果是不同的.在此代码中,您编写了仅当getCall1和getCall2成功时才调用callback1和callback2的方法.这与您的第一个代码不同.

And a last remark about your last code sample, the expected result is not the same. In this code you wrote that you will call callback1 and callback2 only if both getCall1 and getCall2 successed. Which is not the same behaviour as your first code.

编辑:具有真正异步结果的新jsFiddle

Edit : a new jsFiddle with real asynchronous result

http://jsfiddle.net/PG3aN/1/

这篇关于使用deferred和pipe的链回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆