मुझे यह पसंद है कि ब्रैडली ब्रेथवेट अपने ब्लॉग में सुझाव देते हैं :
app
.factory('searchService', ['$q', '$http', function($q, $http) {
var service = {};
service.search = function search(query) {
// We make use of Angular's $q library to create the deferred instance
var deferred = $q.defer();
$http
.get('http://localhost/v1?=q' + query)
.success(function(data) {
// The promise is resolved once the HTTP call is successful.
deferred.resolve(data);
})
.error(function(reason) {
// The promise is rejected if there is an error with the HTTP call.
deferred.reject(reason);
});
// The promise is returned to the caller
return deferred.promise;
};
return service;
}])
.controller('SearchController', ['$scope', 'searchService', function($scope, searchService) {
// The search service returns a promise API
searchService
.search($scope.query)
.then(function(data) {
// This is set when the promise is resolved.
$scope.results = data;
})
.catch(function(reason) {
// This is set in the event of an error.
$scope.error = 'There has been an error: ' + reason;
});
}])
प्रमुख बिंदु:
संकल्प फ़ंक्शन हमारे नियंत्रक में .then फ़ंक्शन से लिंक करता है यानी सब कुछ ठीक है, इसलिए हम अपना वादा रख सकते हैं और इसे हल कर सकते हैं।
अस्वीकार फ़ंक्शन लिंक को हमारे नियंत्रक में .catch फ़ंक्शन से लिंक करता है अर्थात कुछ गलत हो गया है, इसलिए हम अपना वादा नहीं रख सकते हैं और इसे अस्वीकार करने की आवश्यकता है।
यह काफी स्थिर और सुरक्षित है और यदि आपके पास वादे को अस्वीकार करने की अन्य शर्तें हैं, तो आप हमेशा सफलता के समारोह में अपने डेटा को फ़िल्टर कर सकते हैं और deferred.reject(anotherReason)
अस्वीकृति के कारण के साथ कॉल कर सकते हैं ।
जैसा कि रायन वाइस ने टिप्पणियों में सुझाव दिया है , यह तब तक उपयोगी नहीं हो सकता है जब तक कि आप प्रतिक्रिया के साथ थोड़ा सा फील न करें, इसलिए बोलने के लिए।
क्योंकि success
और error
1.4 के बाद से पदावनत कर रहे हैं शायद यह नियमित रूप से वादा तरीकों का उपयोग करने के लिए बेहतर है then
और catch
और उन तरीकों के भीतर प्रतिक्रिया को बदलने और उस तब्दील प्रतिक्रिया का वादा लौट आते हैं।
मैं दोनों दृष्टिकोणों के साथ एक ही उदाहरण दिखा रहा हूं और एक तीसरे इन-बीच दृष्टिकोण:
success
और error
दृष्टिकोण ( success
और error
HTTP प्रतिसाद का एक वादा वापस करें, इसलिए हमें $q
डेटा का वादा वापस करने के लिए सहायता की आवश्यकता है ):
function search(query) {
// We make use of Angular's $q library to create the deferred instance
var deferred = $q.defer();
$http.get('http://localhost/v1?=q' + query)
.success(function(data,status) {
// The promise is resolved once the HTTP call is successful.
deferred.resolve(data);
})
.error(function(reason,status) {
// The promise is rejected if there is an error with the HTTP call.
if(reason.error){
deferred.reject({text:reason.error, status:status});
}else{
//if we don't get any answers the proxy/api will probably be down
deferred.reject({text:'whatever', status:500});
}
});
// The promise is returned to the caller
return deferred.promise;
};
then
और catch
दृष्टिकोण (यह फेंकने के कारण परीक्षण करने के लिए थोड़ा अधिक कठिन है):
function search(query) {
var promise=$http.get('http://localhost/v1?=q' + query)
.then(function (response) {
// The promise is resolved once the HTTP call is successful.
return response.data;
},function(reason) {
// The promise is rejected if there is an error with the HTTP call.
if(reason.statusText){
throw reason;
}else{
//if we don't get any answers the proxy/api will probably be down
throw {statusText:'Call error', status:500};
}
});
return promise;
}
एक आधा समाधान है, हालांकि (इस तरह से आप किसी भी तरह से बच सकते हैं throw
और वैसे भी आपको $q
अपने परीक्षणों में वादा व्यवहार का मजाक उड़ाने के लिए उपयोग करने की आवश्यकता होगी ):
function search(query) {
// We make use of Angular's $q library to create the deferred instance
var deferred = $q.defer();
$http.get('http://localhost/v1?=q' + query)
.then(function (response) {
// The promise is resolved once the HTTP call is successful.
deferred.resolve(response.data);
},function(reason) {
// The promise is rejected if there is an error with the HTTP call.
if(reason.statusText){
deferred.reject(reason);
}else{
//if we don't get any answers the proxy/api will probably be down
deferred.reject({statusText:'Call error', status:500});
}
});
// The promise is returned to the caller
return deferred.promise;
}
किसी भी प्रकार की टिप्पणियों या सुधारों का स्वागत है।
success()
,error()
औरfinally()
किसके साथ संयुक्त होंगेcatch()
? या मुझे इस्तेमाल करना हैthen(successFunction, errorFunction).catch(exceotionHandling).then(cleanUp);