在拒绝或解决之前返回的延迟对象? [英] Deferred object returned before it is rejected or resolved?

查看:77
本文介绍了在拒绝或解决之前返回的延迟对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本上看起来像这样的函数:

I have a function that basically looks like this:

function defTest()
{
    var dfd = new jQuery.Deferred();
    $.ajax(
    {
        type: "GET",
        url: 'http://XXXX',
        cache: false,
        dataType: "json",
        success: function(data,status)
        {
            console.log('ajax done: success');
            dfd.resolve();
        },
        error: function(data,status)
        {
            console.log('ajax done: fail');
            dfd.reject();

        }
    });
    console.log('about to return dfd');
    return dfd;    
}

我这样称呼它:

defTest().then(.....);

控制台日志产生以下内容: 即将返回dfd code.js:106 ajax已完成:成功代码:js:96

The console log produce this: about to return dfd code.js:106 ajax done: success code.js:96

让我感到困惑的是,该代码似乎可以正常工作.仍然在ajax完成之前返回dfd.因此,我删除了返回dfd.并将其放在每个ajax处理函数中的最后,以确保在Ajax完成之前不会返回该值.

What confuses me is that the code seems to work. Still the dfd is returned BEFORE the ajax has finished. So i removed the return dfd. And put it last in each ajax handler function to make sure that this will NOT be returned until the Ajax has finished.:

success: function(data,status)
{
    console.log('ajax done: success');
    dfd.resolve();
    return dfd;
}

然后它根本不起作用.我很迷惑!有人可以向我解释为什么我的延迟对象不能出现在ajax成功和错误处理程序中,为什么即使我的延迟对象即使在Ajax完成之前被解雇然后被解决或拒绝也被返回,即使我的延迟对象返回,它仍然可以工作?那怎么可能?

Then it didn´t work at all. I am confused! Can someone explain to me why my deferred can´t be in the ajax success and error handlers and why it works even though it seems that my deferred object returns even if it is fired BEFORE the Ajax is finished and then be resolved or rejected? How is that even possible?

该问题直接与我以前未解决的更复杂的功能有关: 有关延迟对象的问题

This issue is directly linked to my previous unanswered and more complex function: Problems with deferred object

这就是为什么我不能仅仅返回ajax(...)"的原因,因为我的真实函数包含其他ajax调用,这些调用将与返回给调用方的ONE结果分开.

This is why i can´t just "return ajax(...)" because my real function contains other ajax calls that will be apart of ONE result handed back to the caller.

推荐答案

当然,它马上就返回了.尽管通常可以返回延迟对象并在调用此函数的代码中使用returnDeferredObject.promise().then(),但通常会返回延迟对象的Promise成员.

Of course it's returned right away, that's the point. One would be returning the promise member of the deferred object, normally, though -- although you could return the deferred object and use returnedDeferredObject.promise().then() in code that calls this function.

如果您要进行的处理取决于AJAX的完成,则该处理将在返回的.promise()的.then()函数中进行.这样做的好处在于,在进行异步处理时,您可以执行其他不依赖于AJAX返回的操作.您可以将返回的.promise()传递给其他代码.

If you have processing that's dependent on the AJAX completion, that processing goes in the .then() function of the returned .promise(). The beauty is in that while the asynchronous processing is going on, you can do other things that aren't dependent on the AJAX return. You can pass that returned .promise() around to other code.

var mydata = somethingThatReturnsPromise(args);

// do a whole bunch of things

mydata.then(function (returnedReponse) {
    // do stuff with returnedResponse that doesn't require DOM ready
});

$(function () {
    mydata.then(function (returnedResponse) {
        // do DOM stuff with returnedPromise
    });
});

我希望我不会遗漏您的意思,但是我想将返回的promise视为一个数据源,以后我可以一再使用它,并且我指定的.then()回调仅执行一次有一个returnResponse.

I hope I'm not missing your point, but I like to think of that returned promise as a data source that I can use again and again later on, and the .then() callbacks I specify will only be executed once there's a returnedResponse.

作为旁注,我非常确定,随着jQuery更加朝承诺标准发展,应该将.done()、. pipe()、. fail()和.progress()替换为.then( successCb,failCb,progressCb).

As a side note, I am pretty sure that with jQuery going more towards the promise standard, .done(), .pipe(), .fail() and .progress() should be replaced with use of .then(successCb, failCb, progressCb).

这篇关于在拒绝或解决之前返回的延迟对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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