简体   繁体   English

为什么then()链接的方法不能按顺序运行?

[英]Why is then() chained methods not running sequentially?

We are trying to execute a number of AJAX calls in a particular order. 我们正在尝试以特定顺序执行许多AJAX调用。 The following code has methodA, methodB and methodC (each returns an AJAX promise object running async=true ). 以下代码包含methodA,methodBmethodC (每个返回运行async = true的AJAX Promise对象)。

They are chained using the then() function in jQuery . 它们使用jQuery中then()函数链接。

 self.methodA().then( self.methodB() ).then( self.methodC() )

I have made one of the methods slow ( methodB ) (I use a slow URL). 我已将其中一种方法设为慢速( methodB )(我使用了慢速URL)。

I would expect A ... 10 second wait...then B then C . 我希望A ...等待10秒钟...然后B然后C。

Instead I get A, C ....10 second wait and B . 相反,我得到A,C .... 10秒等待,然后B。

Why is it doing that? 为什么这样做呢? Does it have something to do with me using the alert() in an always() function? 在always()函数中使用alert()与我有什么关系吗?

Here is my code in a fiddle : http://jsfiddle.net/h8tfrvy4/13/ 这是我的小提琴代码: http : //jsfiddle.net/h8tfrvy4/13/

Code: 码:

function Tester() {
    var self = this;
    this.url = 'https://public.opencpu.org/ocpu/library/';
    this.slowurl = 'http://fake-response.appspot.com/?sleep=5';


    this.save = function() {
        self.methodA().then( self.methodB() ).then( self.methodC() )
    }

    this.methodA = function () {
        var self = this;

        return $.ajax({
            url: self.url,
            async: true
        }).always(function (processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {

            //check for errors... and if OK
            alert('A OK');


        })
    }
    this.methodB = function () {
        var self = this;

        return $.ajax({
            url: self.slowurl,
            async: true
        }).always(function (processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {

            //check for errors... and if OK
            alert('B OK');


        })
    }
    this.methodC = function () {
        var self = this;

        return $.ajax({
            url: self.url,
            async: true
        }).always(function (processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
            //OK
            alert('C OK');

        })
    }
}
new Tester().save();

This is wrong: 这是错误的:

self.methodA().then( self.methodB() ).then( self.methodC() )

You're invoking each method immediately , and passing the promises into then . 您将立即调用每种方法,并将promise传递给then

If you want each function to wait until the previous finishes, you need to give each then a callback to execute when the previous promise resolves: 如果你希望每个函数等到前完成,你需要给每个then当先前的承诺,解决了一个回调来执行:

self.methodA().then(function () { return self.methodB() }).then(function() { return self.methodC() });

Short and simple: 简短:

this.save = function() {
    self.methodA().then( self.methodB ).then( self.methodC )
}

It has bothered me all **&^*$& day that @meagar was right and I was wrong on this one but I was sure that I was right. @meagar是正确的,这困扰着我整个**&^ * $&一天,我在这件事上是错的,但是我确信我是正确的。 His answer seemed too complicated and but I was fuzzy headed in the morning and my answer wasn't right either. 他的回答似乎太复杂了,但是早晨我头昏脑胀,我的回答也不对。 This is the right answer and it works perfectly when you plug it into the JSFiddle. 正确的答案,当您将其插入JSFiddle时,它可以完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM