Ember.RSVP.all似乎立即解决 [英] Ember.RSVP.all seems to resolve immediately

查看:531
本文介绍了Ember.RSVP.all似乎立即解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望我正在做一些愚蠢的事,但我似乎找不到。

I'm really hoping that there's something dumb that I'm doing, but I can't seem to find it.

我正在尝试使用Ember .RSVP.all在一个承诺链的中间。我的例子比我的使用简单得多,但是它表明了这个问题。在一个承诺链中间,我有一套承诺,所有这些承诺在链条可以继续之前都需要解决 - 正是我所理解的RSVP。所有的。

I'm trying to use Ember.RSVP.all in the middle of a chain of promises. The example I have is much simpler than my use, but it demonstrates the issue. In the middle of a chain of promises, I have a set of promises that all need to resolve before the chain can continue - exactly what I understand RSVP.all to be for.

不幸的是,当我返回RSVP.all对象时,链中的下一个承诺立即运行,而不用等待传递给all()的承诺。

Unfortunately, when I return the RSVP.all object, the next promise in the chain runs immediately, without waiting for the promises passed to all().

我已经设置了一个js小提琴,以最好的方式展示我可以想到的:
http://jsfiddle.net/3a9arbht/3/

I've set up a js fiddle to demonstrate in the best way that I can think of: http://jsfiddle.net/3a9arbht/3/

请注意,第一和第二个在几乎完全相同的时候解决,当秒应该在1s的承诺回来之后。

Notice that First and Second both resolve at almost exactly the same time, when Second should be after the 1s promise comes back. Third and fourth follow as expected.

小提琴码如下所示:

function delayAjax(delay) {
    return Ember.$.ajax({
        url: '/echo/json/',
        data: {
            json: '',
            delay: delay,
        }
    });
}

delayAjax(1).then(function() {
    Ember.$('#first').addClass('red');
    var proms = [delayAjax(1), delayAjax(1)];
    return Ember.RSVP.all(proms)
}).then(function() {
    Ember.$('#second').addClass('red');
    return delayAjax(1);
}).then(function() {
    Ember.$('#third').addClass('red');
    return delayAjax(1);
}).then(function() {
    Ember.$('#fourth').addClass('red');
});


推荐答案

根据对另一个问题的回应回答。看来,虽然 $。ajax 响应确实是可以的,但它是一个jQuery延迟对象,而不是Promise。我不清楚为什么他们不能在一起玩得很好,但解决方案只是将ajax调用转换为承诺:

Answering based on a response to another question. It appears that while the $.ajax response is indeed "thenable", it is a jQuery deferred object, not a Promise. It's not clear to me why they don't play well together, but the solution is simply to convert the ajax call to a promise:

function delayAjax(delay) {
    return Promise.resolve($.ajax({
        url: '/echo/json/',
        data: {
            json: '',
            delay: delay,
        }
    }));
}

使用工作小提琴: http://jsfiddle.net/evilbuck/vqut9zy2/3/

这篇关于Ember.RSVP.all似乎立即解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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