代码之家  ›  专栏  ›  技术社区  ›  tr3online

与Ember.RSVP的连锁承诺。all和Ember.RSVP。承诺返回奇数结果

  •  2
  • tr3online  · 技术社区  · 10 年前

    当我做以下事情时:

    Ember.$.getJSON(url)
      .then(function(response){
        return Ember.RSVP.all(response.map(Ember.$.getJSON))
      })
      .then(function(response){
        // this is where the oddity begins
        console.log(response)
      })
    

    model 在我的本地环境(Ember 1.13.5)上挂接我的应用程序路由器,我在第二秒得到一个奇怪的响应 then() 的响应,如:

    Promise: {
      _id: 48
      _label: undefined
      _result: Array[1]
      _state: 1
      _subscribers: Array[0]
      __proto__: Promise
    }
    

    我可以做 response.then() 在第二个 then 得到我所期待的回应,但这并不理想,因为我想连锁承诺。

    我尝试在JSBin上设置相同的示例,使用 Ember.run.later 履行承诺: JSBin Example 。这种方法在这里似乎很有效。

    我错过了什么吗?

    1 回复  |  直到 10 年前
        1
  •  2
  •   tr3online    10 年前

    事实证明 Ember.$.ajax() Ember.$.getJSON() 是罪魁祸首。他们正在导致承诺链失败。更换时 getJSON 具有:

    new Ember.RSVP.Promise(function(resolve, reject){
      Ember.run.later(function(){
        console.log('resolving first fake promise');
        var response = [{id: 1, pool: 1, collection: 1}, {id: 2, pool: 2, collection: 1}];
        resolve(response)
      },1000)
    })
    

    它起作用了。所以我去了 ember-cli-ic-ajax ,并用它来处理 获取JSON 部分,而且效果很好。

    干杯,jQuery。干杯

    推荐文章