Javascript return value from a callback function [duplicate]

This question already has an answer here:

I am trying to get the email address of a user logged into chrome using a callback function (chrome.identity.getProfileUserInfo).

I am pretty new to javascript and more of a java developer.
I also tried to return to the main program using the return keyword but that didn’t work either. Any help is greatly appreciated.

var logged_in_user;

chrome.identity.getProfileUserInfo(function(info) {
    logged_in_user =;
    console.log("running...get profile")
    console.log("user email: ",

console.log("wait a min")
console.log("Got user:", logged_in_user);

When I run the code above, the email prints out to the console via the 6th line (console.log("user email: ",, however the result that is printed at the last line (console.log("Got user:", logged_in_user);) in the main program is undefined.

get chrome storage values via local getter setter class but I can’t

I created a local class in JS file with following code :

class CustomChromeStorage {
    //#region userName
    get userName() {

        let isCurrentValueSet = false;'userName', function (obj) {
            this._userName = obj;
            isCurrentValueSet = true;
        while (true) {
            if (isCurrentValueSet) {
                return this._userName;

    set userName(newValue) {
        this._userName = newValue;{ 'userName': newValue }, function () {
    remove_userName() {
        this._userName = null;'userName', function () {
    //#endregion userName

My Idea to do such type of code is when I write somewhere else in my code like :

alert(new CustomChromeStorage().userName);

Then my code simply fetches username from chrome storage and show it via alert. As we know that in order to fetch values from chrome storage we need to create a callback function then value of particular variable is given by particular callback function. I know this is very good practice for asynchronous process but it sometimes becomes cumbersome for me to handle each and every callback function. So, I just want that when I fetch value from chrome storage via my custom class then execution of current code gets paused until response does not get from callback function (like synchronous process). That’s why I written infinite while loop inside getter method of that property but the problem is when I try to alert username via custom chrome storage class my total program execution becomes hang.

The reason behind it is initially I set isCurrentValueSet = false which never gets true inside while loop. If anybody have any idea why it does not set to true inside while loop then please let me know.

Thanks for showing interest into my problem.