Ionic storage promise with if else

Hi

I am going crazy with this new Storage service that returns Promises…

For examle, how do you convert this code (Ionic 1)

           $scope.isUser = UserService.getUser() ? true : false;
                $scope.userTemp = storageService.get("userTemp  ") ? true : false;


                $scope.data.showLoginClose = ($scope.isUser || $scope.userTemp || !$rootScope.isMobile );
                if ($scope.isUser) {
                    $scope.user = UserService.getUser();
                    switch ($scope.user.authVia) {
                    case 'google':
                        $scope.googleLoginButtonShow = false;
                        $scope.googleLogoutButtonShow = true;
                        $scope.facebookLoginButtonShow = false;
                        $scope.facebookLogoutButtonShow = false;
                        break;
                    case 'FB':
                        $scope.googleLoginButtonShow = false;
                        $scope.googleLogoutButtonShow = false;
                        $scope.facebookLoginButtonShow = false;
                        $scope.facebookLogoutButtonShow = true;
                        break;
                    default:
                    }
                }
                if ( $rootScope.isMobile && !$scope.isUser && storageService.get('onboardingDone') === 'yes' ) {
                    $scope.modal.show();
                }

to ionic 2 using Storage ?

Here’s my take, but I am not happy with it, it’s long and complicated, I need help writing something cleaner:

    let isUser: boolean;
	let userTemp: boolean;
	let that = this;
	this.storage.ready().then(() => {
		this.storage.get('userTemp')
			.then((val) => {
				userTemp = true ;
				continueShit(userTemp);
			})
			.catch( (e) => {
				userTemp = false ;
				continueShit(userTemp);
			})
	});

	function continueShit(userTemp){
		let isUser = that.UserService.getUser() ? true : false;
		that.data.showLoginClose = (isUser || userTemp || !that.isCordova.f() );
		if (isUser) {
			let user = that.UserService.getUser();
			switch (user.authVia) {
				case 'google':
				that.googleLoginButtonShow = false;
				that.googleLogoutButtonShow = true;
				that.facebookLoginButtonShow = false;
				that.facebookLogoutButtonShow = false;
				break;
				case 'FB':
				that.googleLoginButtonShow = false;
				that.googleLogoutButtonShow = false;
				that.facebookLoginButtonShow = false;
				that.facebookLogoutButtonShow = true;
				break;
				default:
			}
		}

		that.storage.get('onboardingDone').then( (val) => {
			if (val === "yes" && that.isCordova.f() && !isUser ){
				that.navCtrl.push(LogoutPage);
			}
		})
	}

When you think in terms of “how do I convert this synchronous code to use asynchronous sources?”, you lock yourself into fighting the framework. Let go of the original structure and be reactive:

class UserService {
  constructor(private _storage: Storage) {
  }

  isUserTemp(): Promise<boolean> {
    return this._storage.ready().then(() => this._storage.get('userTemp'));
  } 

  getUser(): Promise<User> {
     return this._storage.ready().then(() => this._storage.get('user'));
  }
}

class WhateverPage {
  constructor(private _users: UserService) {
    _users.isUserTemp().then((isTemp) => {
      if (isTemp) {
        _users.getUser().then((user) => { 
          // do stuff using user: it is only valid in here
        });
      }
    });
  }
}
1 Like

Yes ok, I see what you mean !

Thanks for the code