javascriptember.jspromisechainingrsvp.js

Chaining promises with Ember.RSVP.all and Ember.RSVP.Promise returning odd results


When I do something like:

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)
  })

in the model hook in my application router on my local environment (Ember 1.13.5), I get a weird response, in the second then()'s response, like:

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

I can do a response.then() in the second then to get the response I'm looking for, but that is less than ideal since I would like to chain promises.

I tried to set up the same example on JSBin, using Ember.run.later to act as promises: JSBin Example. The methodology seems to work fine here.

Am I missing something?


Solution

  • As it turns out Ember.$.ajax() and Ember.$.getJSON() are the culprits. They are causing the promise chain to fail. Upon replacing getJSON with:

    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)
    })
    

    It works. So I head on over to ember-cli-ic-ajax, and used that to handle the getJSON portion, and it works fine.

    Cheers, jQuery. Cheers.