10110
10110

Reputation: 2675

Calling a JS Function with Axios inside it Returns Undefined when called

The function isAdmin('foo') will return true or false if user with alias foo has any of the specified roles bound to it.

Here's the code:

export function isAdmin(alias, adminRoleOverride) {
try {
    axios.get('https://xxxx.execute-api.us-west-2.amazonaws.com/xxxx/xxxx/' + alias)
    .then(function (response) {
        var admin = false;
        var aliasBoundRoles = response.data; //An array with the roles the alias currently holds.

        var adminRolePolicy = ['SuperAdmin', 'Admin', 'Director', 'RegionalManager',
            'TrainingManager', 'SiteTrainer', 'SitePOC', 'OutSourceSitePocManager']; //What is considered an admin.
        if(adminRoleOverride){
            adminRolePolicy = adminRoleOverride;
        } //If an array with roles is passed as param, override the default adminRolePolicy.

        admin = aliasBoundRoles.some((role) => {
            return adminRolePolicy.includes(role);
        }); //If any of the aliasBoundRoles is in the adminRolePolicy return true else false.
        return admin;
    });
} catch (error) {
    console.error("Error when attempting to authorize user " + alias + "."
    + "\nError: " + error);
    return false;
}  
}

I would like to use the function as follows:

if(isAdmin('foo')){
    console.log("YAAAY")
}

.. but it wont work cause when the if-block gets evaluated isAdmin('foo') is still undefined, hasn't returned.

I know it has to do something with the fact that the axios call is async and takes time to pull the data.

How can I get this to work, any help would be greatly appreciated and if you have any tutorials about this I'd appreciate it.

Upvotes: 0

Views: 35

Answers (2)

ehab
ehab

Reputation: 8044

you wanna do the following

export function isAdmin(alias, adminRoleOverride) {
try {
    // return the promise
    return axios.get('https://xxxx.execute-api.us-west-2.amazonaws.com/xxxx/xxxx/' + alias)
    .then(function (response) {
        var admin = false;
        var aliasBoundRoles = response.data; //An array with the roles the alias currently holds.

        var adminRolePolicy = ['SuperAdmin', 'Admin', 'Director', 'RegionalManager',
            'TrainingManager', 'SiteTrainer', 'SitePOC', 'OutSourceSitePocManager']; //What is considered an admin.
        if(adminRoleOverride){
            adminRolePolicy = adminRoleOverride;
        } //If an array with roles is passed as param, override the default adminRolePolicy.

        admin = aliasBoundRoles.some((role) => {
            return adminRolePolicy.includes(role);
        }); //If any of the aliasBoundRoles is in the adminRolePolicy return true else false.
        return admin;
    });
} catch (error) {
    console.error("Error when attempting to authorize user " + alias + "."
    + "\nError: " + error);
    return Promise.resolve(false);
}  
}

and later

// this code should be inside an async function
const hasRoleAdmin = await isAdmin('foo')
if(hasRoleAdmin){
    console.log("YAAAY")
}

Upvotes: 2

Jacob
Jacob

Reputation: 78840

The easiest way to get an async result when Promises are involved is to use an async function and await the result:

async function codeDoingTheCheck() {
  const isAuthorized = await isAdmin('foo');
  if (isAuthorized) {
    console.log("YAAAY")
  }
}

Keep in mind that codeDoingTheCheck is now also async, so whatever calls it has to await its result (and whatever calls that, etc.)

Upvotes: 1

Related Questions