javascript - AngularJs : Callback or promise in a service using conditions -
i have specific case i'm not sure if should implement callback or promise. i'm totally new promises , starting understand concept. therefor i'd not fall anti patterns.
i did read , re-read angular doc $q.
so implement :
if using promises :
oauthservice.token().then(function(access_token){ config.url = config.url+'?access_token='+access_token; }); return config;
if using callback :
oauthservice.token(function(access_token){ config.url = config.url+'?access_token='+access_token; }); return config;
oauth service not http request if has conditions in let me think should use callback instead of promises.
so here service using callback now.
oauthservice.js :
app.factory('oauthservice', function($http, $localstorage) { return { token : function(callback){ // actual token access_token = $localstorage.getobject('access_token'); // actual identity identity_token = $localstorage.getobject('identity_token'); // if no user logged if(isobjectempty(identity_token)){ // if access_token not exist or expires if( isobjectempty(access_token) || date.now() > (access_token.expires_at - (600*1000)) ){ // create anonymous access_token $http .get(domain+'/oauth/v2/token?client_id='+public_id+'&client_secret='+secret+'&grant_type=client_credentials') .then(function (response) { $localstorage.setobject('access_token', { key: response.data.access_token, type: 'anonymous', expires_at: date.now()+(response.data.expires_in*1000) }); // return access token here callback(response.data.access_token); }); } } // if user logged else{ // if access_token not exist or expires or anonymous if( isobjectempty(access_token) || date.now() > (access_token.expires_at - (600*1000)) || access_token.type == 'anonymous' ){ // create access_token identity $http .get(domain+'/oauth/v2/token?client_id='+public_id+'&client_secret='+secret+'&api_key='+identity_token+'&grant_type=http://oauth2.dev/grants/api_key') .then(function (response) { $localstorage.setobject('access_token', { key: response.data.access_token, type: 'identity', expires_at: date.now()+(response.data.expires_in*1000) }); // return access token here callback(response.data.access_token); }); } } // return access token here (if previous token has not changed of type or expired) callback(access_token.key); } }; })
so if i'd rather go promises how should implement that?
having conditions in operation has nothing callbacks vs. promises. plain callbacks crappy way asynchronous operations , should use promises whenever can.
you can rewrite token
method use promises this:
app.factory('oauthservice', function($http, $localstorage, $q) { return { token : function(callback){ // actual token access_token = $localstorage.getobject('access_token'); // actual identity identity_token = $localstorage.getobject('identity_token'); // if no user logged if(isobjectempty(identity_token)){ // if access_token not exist or expires if( isobjectempty(access_token) || date.now() > (access_token.expires_at - (600*1000)) ){ // create anonymous access_token return $http .get(domain+'/oauth/v2/token?client_id='+public_id + '&client_secret=' + secret + '&grant_type=client_credentials') .then(function (response) { $localstorage.setobject('access_token', { key: response.data.access_token, type: 'anonymous', expires_at: date.now() + (response.data.expires_in * 1000) }); return response.data.access_token; }); } } // if user logged else { // if access_token not exist or expire or anonymous if( isobjectempty(access_token) || date.now() > (access_token.expires_at - (600*1000)) || access_token.type == 'anonymous' ){ // create access_token identity return $http .get(domain+'/oauth/v2/token?client_id='+public_id+'&client_secret='+secret + '&api_key='+identity_token+'&grant_type=http://oauth2.dev/grants/api_key') .then(function (response) { $localstorage.setobject('access_token', { key: response.data.access_token, type: 'identity', expires_at: date.now()+ (response.data.expires_in * 1000) }); return response.data.access_token; }); } } // return access token here (if previous token has not changed of type or expired) return $q.when(access_token.key); } }; });
and can bit of refactoring reduce this:
app.factory('oauthservice', function($http, $localstorage, $q) { function expiressoon(access_token) { return date.now() > (access_token.expires_at - (600*1000)); } function getaccesstoken(url, type) { return $http .get(url) .then(function (response) { $localstorage.setobject('access_token', { key: response.data.access_token, type: type, expires_at: date.now() + (response.data.expires_in * 1000) }); return response.data.access_token; }); } return { token : function(callback){ // actual token access_token = $localstorage.getobject('access_token'); // actual identity identity_token = $localstorage.getobject('identity_token'); // if no user logged if(isobjectempty(identity_token)){ // if access_token not exist or expires if( isobjectempty(access_token) || expiressoon(access_token) ) { var url = domain + '/oauth/v2/token?client_id=' + public_id + '&client_secret=' + secret + '&grant_type=client_credentials'; // create anonymous access_token return getaccesstoken(url, 'anonymous'); } } // if user logged else { // if access_token not exist or expire or anonymous if( isobjectempty(access_token) || expiressoon(access_token) || access_token.type == 'anonymous' ){ var url = domain+'/oauth/v2/token?client_id=' + public_id+ '&client_secret='+secret + '&api_key='+identity_token + '&grant_type=http://oauth2.dev/grants/api_key'; // create access_token identity return getaccesstoken(url, 'identity'); } } // return access token here (if previous token has not changed of type or expired) return $q.when(access_token.key); } }; });
Comments
Post a Comment