2017-03-09 42 views
0

APIからデータを取得するサービスがあります。私はこのサービスを呼び出そうとしています。それは同じ価値を持って帰ってきている。defer.promiseはangularJSで同じ結果を返します

appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) { 
    var self = this; 
    self.getCustomerData = function(token,name) { 
     var deferred = $q.defer(); 
     return $http({ 
      method: 'GET', 
      url: , 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }).then(function(response) { 
      deferred.resolve(response); 
      return deferred.promise; 
     }, function(response) { 
      deferred.reject(response); 
      return deferred.promise; 
     }); 
    }; 
}]); 
+3

「同じ値が」どういう意味:

通常フェッチは次のようになりますか?今、 'return deferred.promise'行は何もしません。 '$ http'約束を返してください。延期の必要はありません。 –

+0

'return deferred.promise;'行を '.then()'の成功関数と失敗関数の外に置こうとしましたか? –

+0

@MikeMcCaughan同じ値は、APIの応答が以前のAPI呼び出しと同じであることを意味します。 – Player

答えて

0

私はここで少し混乱しています。それをクリアしようとしましょう。あなたは繰延オブジェクトを使用する場合は、あなたのコードビットを変更する必要があります。具体的には

appName.service('FetchCustomerDate', ['$http', '$q', function ($http, $q) { 
    var self = this; 
    self.getCustomerData = function (token, name) { 
     var deferred = $q.defer(); 
     $http({ // Do not return here, you need to return the deferred.promise 
      method: 'GET', 
      url: '...some URL here...', 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }).then(function (response) { 
      deferred.resolve(response); // It's correct, you are resolving the deferred promise here. 
      // return deferred.promise; // You do not need to return the deferred.promise here. 
     }, function (response) { 
      deferred.reject(response); // It's correct, you are rejecting the deferred promise here. 
      // return deferred.promise; // You do not need to return the deferred.promise here. 
     }); 

     return deferred.promise; // The function must return the deferred.promise 
    }; 
}]); 

、機能getCustomerDatareturn deferred.promisedeferredオブジェクトに属する約束を返す必要があります。 then()コールバック内では、deferred約束を解決または拒否するだけです。 deferred.promiseを返す必要はありません。

コードを改善することができます。 $httpサービスは約束を返し、thenコールバックによって返された値は約束でthenメソッドでラップされます。あなたがdeferredオブジェクトの使用削除することができ、ことを知っ:あなたが見ることができるように

appName.service('FetchCustomerDate', ['$http', function ($http) { 
    var self = this; 
    self.getCustomerData = function (token, name) { 
     return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response returned inside "then" callbacks. 
      method: 'GET', 
      url: '...some URL here...', 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }).then(function (response) { 
      return response; // Simply return the response, it will be wrapped in a resolved promise by "then()" 
     }, function (response) { 
      return response; // Simply return the response, it will be wrapped in a rejected promise by "then()" 
     }); 
    }; 
}]); 

を、2つのthenコールバックは、単にあなたがそれらを省略することができ、この理由のため、responseオブジェクトを返します。

appName.service('FetchCustomerDate', ['$http', function ($http) { 
    var self = this; 
    self.getCustomerData = function (token, name) { 
     return $http({ // Here, you need to return the promise returned by $http. Than promise will contain the response form the GET call 
      method: 'GET', 
      url: '...some URL here...', 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }); 
    }; 
}]); 
+0

ありがとうございます – Player

0

通常$httpサービスでデータを取得する場合は、レスポンスからデータを取得し、たとえば$scopeに影響を与えたり、何とか処理したりする必要があります。あなたは何をしようとしているのですか?あなたの質問を明確にしてください。

appName.service('FetchCustomerDate', ['$http', '$q', function($http, $q) { 
    var self = this; 

    function notifyError(reason) { 
     console.error(reason); 
    } 

    self.getCustomerData = function(token,name) { 
     var deferred = $q.defer(); 
     return $http({ 
      method: 'GET', 
      url: , 
      headers: { 
       "Authorization": token, 
       "x-xcmc-auth": '' 
      } 
     }) 
      .then(function onSuccess(response) { 
      var cfg = response.data; // process data 
     }) 
      .then(function onSuccess(response) { 
      // chained promises 
     }) 
      .then(
      function onSuccess(res) { 
       // ... this will trigger the chain reaction 
       deferred.resolve(res); 
      }, 
      function onFailure(reason) { 
       notifyError(reason); // manage the error 
       deferred.reject(reason); 
      }) 
      ; 
      return deferred.promise; 
     } 
    }]); 
関連する問題