Non*_*Non 5 javascript promise angularjs
这样做可以吗?
例如,我有我的退出服务
logout: function() {
var defer = $q.defer();
this.getCustomer().then(function(credentials) {
$http.post(CONSTANT_VARS.BACKEND_URL + '/auth/logout',
{username: credentials.username, customer: credentials.customer}
).success(function(data) {
if (data.error) {
defer.reject(data);
}
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_CUSTOMER).then(function() {
/*Removing LocalForage Items*/
cleanLocalForage();
defer.resolve(data);
}, function(err) {
console.log(err);
defer.reject(data);
});
}).error(function(data) {
cleanLocalForage();
defer.reject(data);
});
}, function(err) {
defer.reject(err);
});
return defer.promise;
},
Run Code Online (Sandbox Code Playgroud)
然后我在控制器中有一个函数,一旦会话到期就会返回一个错误.当会话到期时,我需要注销用户并将其重定向到登录路径.所以,这就是我到目前为止所拥有的:
$scope.removeSlip = function(slip) {
BetSlipFactory.removeSlip(slip).then(function() {
}, function(err) {
console.log(err);
AuthFactory.logout();
$location.path('/');
});
};
Run Code Online (Sandbox Code Playgroud)
或者我应该通过注销承诺做这样的事情BetSlipFactory.removeSlip()吗?
$scope.removeSlip = function(slip) {
BetSlipFactory.removeSlip(slip).then(function() {
}, function(err) {
console.log(err);
AuthFactory.logout().then(function() {
$location.path('/');
})
});
};
Run Code Online (Sandbox Code Playgroud)
有:在这种情况下,正确的方法是什么?