Mads K
Mads K

Reputation: 837

Make promise dependent on another promise in Angular

I have this factory which requires a promise in GlobalService.projectJson() to be resolved before the next promise which we returns to the controller with return query.find();(query.find() returns a promise) How would i do that without ending up with pyramid-code?

  .factory('DataService', function (GlobalService) {
    Parse.initialize(parseblah,parseblah);

    return {
      getEntityData: function(name) {
        GlobalService.projectJson().then(function(result) {
            var Entity = Parse.Object.extend(result.login + "_" + name);
            var query = new Parse.Query(Entity);
            query.descending("createdAt");
            return query.find();
        })
      }
    };
  });

Upvotes: 1

Views: 473

Answers (1)

Chandermani
Chandermani

Reputation: 42669

I believe you are just missing a return statement for your getEntityData method.

return {
      getEntityData: function(name) {
        return GlobalService.projectJson().then(function(result) {
            var Entity = Parse.Object.extend(result.login + "_" + name);
            var query = new Parse.Query(Entity);
            query.descending("createdAt");
            return query.find();
        })
      }
    };

Now the return value is a promise itself, which gets resolved with the return value for query.find()

Upvotes: 1

Related Questions