ব্র্যাডলি ব্রেথওয়েট তার ব্লগে যেমন পরামর্শ দেয় আমি এটি করি :
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;
});
}])
গুরুত্বপূর্ণ দিক:
সমাধান নিয়ন্ত্রণের ফাংশনটি আমাদের নিয়ামকটিতে .টির ফাংশনের সাথে লিঙ্ক করে অর্থাৎ সমস্ত কিছু ঠিক আছে, তাই আমরা আমাদের প্রতিশ্রুতি রাখতে পারি এবং এটি সমাধান করতে পারি।
আমাদের নিয়ামকটিতে। ক্যাচ ফাংশনের সাথে প্রত্যাখাত ফাংশন লিঙ্কগুলি অর্থাত্ কিছু ভুল হয়ে গেছে, তাই আমরা আমাদের প্রতিশ্রুতি রাখতে পারি না এবং এটি প্রত্যাখ্যান করার প্রয়োজন নেই।
এটি বেশ স্থিতিশীল এবং নিরাপদ এবং যদি প্রতিশ্রুতি প্রত্যাখ্যান করার অন্যান্য শর্ত থাকে তবে আপনি সর্বদা সাফল্য ফাংশনে আপনার ডেটা ফিল্টার করতে পারেন এবং deferred.reject(anotherReason)
প্রত্যাখ্যানের কারণ সহ কল করতে পারেন।
রায়ান ভাইস মন্তব্যগুলিতে যেমন পরামর্শ দিয়েছেন , আপনি প্রতিক্রিয়াটি কিছুটা ভাবেন না, তবে কথা বলতে না পারলে এটি কার্যকর হিসাবে দেখা যাবে না।
কারণ success
এবং error
১.৪ থেকে অবহেলিত হতে পারে নিয়মিত প্রতিশ্রুতি পদ্ধতি ব্যবহার করা then
এবং catch
সেই পদ্ধতিগুলির মধ্যে প্রতিক্রিয়াটি রূপান্তর করা এবং সেই রূপান্তরিত প্রতিক্রিয়ার প্রতিশ্রুতি ফিরিয়ে দেওয়া ভাল।
উভয় পদ্ধতির সাথে এবং তৃতীয় অভ্যন্তরীণ পদ্ধতির সাথে আমি একই উদাহরণটি প্রদর্শন করছি:
success
এবং error
যোগাযোগ করুন ( success
এবং error
এইচটিটিপি প্রতিক্রিয়ার প্রতিশ্রুতি ফিরিয়ে দিন, সুতরাং আমাদের $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);