An Exercise for a Boolean Based Promise

This is an exercise I went through because I could not understand why an async/await function that returns a boolean promise resolved to true or false but when logging the value to the console it still returned an object instead of the true or false value I was expecting.

Say we have a basic function that returns true or false

isLoggedIn() {
    return condition ? true : false;
}

Will this async function resolve the same boolean value of true or false?

async isLoggedIn(): Promise<boolean> {
    return (await this.storageService.get('isLoggedIn')) ?? false;
}

The isLoggedIn() function and the async isLoggedIn() function with await have different behaviors.

The basic function isLoggedIn() simply returns the value true or false based on the condition.

On the other hand, the async isLoggedIn() function with await will return a promise that resolves to a boolean value.

WTF? The promise still needs to be resolved when you use it, is it not already handled?.

Apparently not!

async canActivate() {
    if (! await this.userService.isLoggedIn()) {
        return false;
    }
    return true;
}
canActivate() {
    return this.userService.isLoggedIn()
        .then((isLoggedIn) => {
            if (!isLoggedIn) {
                return false;
            }
            return true;
        }).catch((error) => {
            console.error(error);
            return false;
        });
}