简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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