This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.3k
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
$q: check if promise is resolved #8307
Copy link
Copy link
Closed
Description
I think it's probably easiest to describe the use case:
When the user clicks a button, a GET request for some resource is sent to the server.
- The user clicks the button
- The user clicks the button again before the AJAX request has returned
I don't want to issue 2 requests for the same data, so I return the original promise to the second request. For example:
factory('DataService', ['$http', '$q', function($http, $q) {
var load_data = false;
return {
/**
* This function is unchanged when called multiple times - so it is
* safe to just return the same promise as for a prev call
*/
load: function(id) {
if(load_data) return load_data.promise;
load_data = $http({
method: 'GET',
url: '/some/resource/'+id,
});
return load_data.promise;
}
};
}]);
The above code won't work in the case where the promise has returned, I see the ideal solution as the ability to do this:
factory('DataService', ['$http', '$q', function($http, $q) {
var load_data = false;
return {
/**
* This function is unchanged when called multiple times - so it is
* safe to just return the same promise as for a prev call
*/
load: function(id) {
if(load_data && !load_data.resolved) return load_data.promise;
load_data = $http({
method: 'GET',
url: '/some/resource/'+id,
});
return load_data.promise;
}
};
}]);
The stack overflow question here: http://stackoverflow.com/questions/17885979/how-to-check-whether-an-angular-q-promise-is-resolved discusses the situation.
What are your thoughts on this? (Apologies if this is a duplicate - I did look and couldn't find anything)
Metadata
Metadata
Assignees
Labels
No labels