多个 API 请求和 javascript 映射函数中的 Object.assign

Multiple API request and Object.assign in javascript map function

提前致谢。我将合并 return 值但失败了。

有 3 个 API 请求和 3 个 return 值:

第一个:

https://gateway.trybindo.com/v2/stores/382/purchase_orders/17157

Return 值:

{purchase_order:
  {purchase_items: [{id: 01, source_id: 24308265}, {id:02, source_id: 3820202}]  }
}

第二个:

https://trybindo.com/api/v2/stores/382/listings/ + 'source_id'

Return 值:

// source_id = 24308265
{listing:  {base_unit: {unit_group_id: 345} }}

// source_id = 3820202
{listing:  {base_unit: {unit_group_id: 308} }}

第三个:

https://trybindo.com/api/v2/stores/382/unit_groups/+ 'unit_group_id'

Return 值:

{unit_group:
 id: 345,
 units: [{name: 'mg'}, {name: 'g'}, {name: 'kg'}]
}

{unit_group:
 id: 308,
 units: [{name: 'unit name test1'}]
}

我想要的是:

{purchase_order:
   {purchase_items: [
     {id: 01, source_id: 24308265, units: ['mg','g','kg']}, 
     {id:02, source_id: 3820202, units:['unit name test1']}
   ]}
}

当我请求一件商品时效果很好:

var getPurchaseOrder = function(id, store_id) {
  $http.get(URL01)
    .success(function(data) {
      let purchase_items = data.purchase_order.purchase_items;

      $http.get(URL02 + purchase_items[0].source_id)
        .success(function(listing){

        $http.get(URL03 + unit_group_id)
        .success(function(units){
          let unitsArray = units.unit_group.units.map(v=>v.name)
          purchase_items[0] = Object.assign({}, purchase_items[0], {units: unitsArray})
          deferred.resolve( data.purchase_order );
        })
      })
    })
  return deferred.promise;
};

但是当我尝试使用地图功能时 Object.assign 它失败了:

var getPurchaseOrder = function(id, store_id) {
  $http.get(URL01)
    .success(function(data) {
      let purchase_items = data.purchase_order.purchase_items;

      purchase_items.map(purchase_item => {

        $http.get(URL02 + purchase_item.source_id)
          .success(function(listing){
          $http.get(URL03 + unit_group_id)
          .success(function(units){
            let unitsArray = units.unit_group.units.map(v=>v.name)
            purchase_item = Object.assign({}, purchase_item, {units: unitsArray})
            deferred.resolve( data.purchase_order );
          })
        })
      })
    })
  return deferred.promise;
};

我该如何解决这个问题?

这个问题可能已经在这里有了答案: 在地图中调用异步函数的最佳方式?

相差angularjs,所以解为:

 var getPurchaseOrder = function(id, store_id) {
    var deferred = $q.defer();
    $http.get($rootScope.gateway + '/v2/stores/' + store_id + '/purchase_orders/' + id)
      .success(function(data) {
        let purchase_items = data.purchase_order.purchase_items;
        let promises = purchase_items.map((item) => {
          let innerDefer = $q.defer()
          $http.get($rootScope.api+'/api/v2/stores/'+ store_id + '/listings/' + item.source_id)
            .success((listing) => {
              $http.get($rootScope.api+'/api/v2/stores/'+ store_id + '/unit_groups/'+ listing.listing.unit_group_id)
              .success(function(units){
                let unitsArray = units.unit_group.units.map(v=>v.name)
                item.units = unitsArray
                innerDefer.resolve( item );
              })
            })
          return innerDefer.promise
        })
        $q.all(promises).then((purchase_items) => {
          data.purchase_order.purchase_items = purchase_items
          deferred.resolve(data.purchase_order)
        })
      })
      .error(function(err) {
        deferred.reject(err);
      });
    return deferred.promise;
  };