Using Ionic Storage has been quite challenging for me as it returns a Promise, which I find to be impractical.
I am trying to retrieve a value from Storage and based on its boolean value, perform certain actions.
In my old Angular 1 code, I had the following:
$scope.isUser = UserService.getUser() ? true : false;
$scope.isUserTemp = storageService.get("userTemp") ? true : false;
$scope.data.showLoginClose = ($scope.isUser || $scope.isUserTemp || !$rootScope.isMobile );
Below is my new TypeScript implementation:
let isUser: boolean;
let isUserTemp: boolean;
this.storage.ready().then(() => {
this.storage.get('userTemp').then((val) => {
isUserTemp = true ;
isUser = UserService.getUser() ? true : false;
this.data.showLoginClose = (isUser || userTemp || !this.isCordova.f() );
})
.catch( (e) => {
isUserTemp = false ;
isUser = UserService.getUser() ? true : false;
this.data.showLoginClose = (isUser || userTemp || !this.isCordova.f() );
})
});
It seems like the new code is much longer compared to the old one, with 14 lines instead of just 3.
Could someone assist me in creating a cleaner Promise-based solution in Angular2 / TypeScript that covers all scenarios (e.g. when isUser is true, userTemp is true; when isUser is false, userTemp is true; etc.)?