$q.all(promises)and structure of promises object t

2019-09-03 11:42发布

I am using Angularjs $q.all(promises) to make multiple REST call and then collecting the data once promise is successful. I have following following.

  1. If "promises is simple array then it works Plunker

    var promises = [
       Users.query().$promise,
       Repositories.query().$promise
    ];
    
  2. If "promises" is simple object then also it works Plunker

    var promises = {
       users: Users.query().$promise,
       repos: Repositories.query().$promise
    };
    
  3. If "promises" is nested object then it is not working. For my requirement I need nested object to remember the input parameters. Plunker

    var promises = {
       users: {"phx":Users.query().$promise},
       repos: {"phx":Repositories.query().$promise}
    };
    

These plunkr are just to simulate my problem. However I want this approach in real project for following requirement.

  1. I have list of 12 product

  2. Each product has "details", "benefits" and "offers" data

  3. I have separate REST API services for "details", "benefits" and "offers" having :productID as parameter
  4. I am making call in following order

    a. Loop for each cards

    b. For each card, make a REST API call for "details", "benefits" and "offers"

    c. Add #b steps into "promises" object

    d. call

    $q.all(promises).then(function(results) {
        // Here need logic to compile the result back to product 
        // and corresponding  "details", "benefits" and "offers" mapping
      } 
    

    and get the data back

Following is json structure I needed to collect my response.

{
    "prod1": {
      "benefits": {},
      "offers": {},
      "pages": {
        "productPage": {}
      }
    }
  },
  "common": {
    "benefits": {},
    "pages": {
      "commonBenefit": {}
    },
    "others": {}
  }

How can I achieve this?

1条回答
在下西门庆
2楼-- · 2019-09-03 11:58

If you really need it, you can wrap the nest with $q.all like this:

var promises = {
  users: $q.all({"phx": Users.query().$promise}),
  repos: $q.all({"phx": Repositories.query().$promise})
};

plnkr.co

查看更多
登录 后发表回答