jQuery的时候/再(也时/完成)不候 [英] jquery when/then (also when/done) not waiting

查看:75
本文介绍了jQuery的时候/再(也时/完成)不候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着使用此语法许多样品,但我看不到我米做错了的,那么功能的AJAX调用返回前运行。

我已经试过也使用$ .deferred和一些其他模式都无济于事。

有人能看到我错过了什么?

我已经调试,可以看到正在从内部呼叫完成/然后Ajax调用返回之前,它的成功(或错误)的数据。

感谢您的帮助。

主要电话:

  this.addFirstTask =功能(任务){
    变种totalHours = GetHours();
    $。当(totalHours).done(功能(数据){
        task.TaskHours(数据);
        self.add(任务);
    });
};
 

这是调用下面的AJAX功能:

 函数GetHours(){
    $阿贾克斯({
        网址:/ API /尖沙​​咀/ GetMyData
        键入:GET,
        数据类型:JSON,

        成功:功能(数据){
           返回的数据;
        },

        错误:功能(数据){
            返回0;
        }
    });
};
 

谢谢!

验尸:

除了增加返回Ajax调用,按照其他建议,我删除了成功和放大器;从AJAX调用错误,改变addFirstTask为:

  this.addFirstTask =功能(任务){
    变种totalHours = GetHours();
    $。当(totalHours)
     。然后(功能(数据){task.TaskHours(数据);})
     .done(函数(){self.add(任务);});
};
 

如果GetHours成功,我更新TaskHours值。如果失败,我只是跳过它。我意识到,做的是像终于在.net中的try / catch语句。因此,而不是将任务马上,然后让我观察到的绑定更新它,当值回来,连self.add是异步调用的一部分。

解决方案

 函数GetHours(){
    返回$阿贾克斯({
        ...
    });
};
 

$。阿贾克斯()返回一个承诺。 $。当()接受承诺的集合,并返回时,所有的输入承诺完全完成一个承诺 。那么()是对承诺的方法(在这种情况下,包装的承诺,当()创建)调用它的第一个功能ARG时承诺解决与成功。

那么,你的GetHours需要返回一个承诺,当()可以包装,并调用则()上。 你实际上可以跳过时,()的一部分,只需调用。这时从GetHours的回报。

你没有得到一个错误,因为。当()也需要任何旧的对象,如果不履行承诺的接口,它只是把它像一个已经完成的承诺。

I've looked at many samples that use this syntax, but I can't see what I"m doing wrong. The "then" function is running before the ajax call returns.

I've tried also using $.deferred and a few other patterns to no avail.

Can someone see what I'm missing?

I have debugged and can see the calls being made from inside the done/then before the ajax call returns it's success (or error) data.

Thanks for any help.

Main call:

this.addFirstTask = function(task) {
    var totalHours = GetHours();
    $.when(totalHours).done(function (data) {
        task.TaskHours(data);
        self.add(task);
    });
};

It is calling the following ajax function:

function GetHours() {
    $.ajax({
        url: "/api/tst/GetMyData",
        type: 'GET',
        dataType: 'json',

        success: function(data) {
           return data;
        },

        error: function(data) {
            return 0;
        }
    });
};

Thanks!

post mortem:

in addition to adding the return to the ajax call, as per additional advice, I removed success & error from the ajax call and changed addFirstTask to:

this.addFirstTask = function(task) {
    var totalHours = GetHours();
    $.when(totalHours)
     .then(function (data) {task.TaskHours(data);})
     .done(function () { self.add(task); });
};

If GetHours succeeds, I update the TaskHours value. If it fails, I just skip it. I realized that DONE is like a "FINALLY" in .NET's try/catch. So rather than adding the task right away and then letting my observable binding update it when the value comes back, even the self.add is part of the asynchronous call.

解决方案

function GetHours() {
    return $.ajax({
        ...
    });
};

$.ajax() returns a Promise. $.when() takes a collection of Promises, and returns a promise that completes when all the input promises complete .then() is a method on Promise (in this case, the wrapper Promise that when() created) that calls it's first function arg when the promise resolves with success.

so, your GetHours needs to return a Promise that when() can wrap, and you call the then() on. you could actually skip the when() part and just call .then on the return from GetHours.

you aren't getting an error because .when() also takes any old object, and if it doesn't implement the Promise interface, it just treats it like an already-completed Promise.

这篇关于jQuery的时候/再(也时/完成)不候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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