I have defined three functions, which all use the Promise to return some values. The first function is to return the token by calling the database API, the second function is to return all the databases by using the token returned from the first function, the third function is to get the account access by using the info from previous two functions

```
function get_auth_token(){
    const  api_end_point = nebula_staging_url + "/api/v1/auth"

    var options = {
        url: api_end_point,
        headers: {"Content-Type": "application/json"},
        auth: {
            'user': 'admin',
            'pass': 'password'}
    }

    return new Promise((resolve, reject) => {
        request.get(options, function(error, response, body){
        if(!error && response.statusCode==200){
            var token = JSON.parse(body)
            var auth_token = token["data"][0]["accessToken"]
            // console.log(auth_token)
            resolve(auth_token)

        }
        else{
            reject(error)
        }
    })
})
}

function get_all_tenants(access_token){
    var api_end_point = nebula_staging_url + "/api/v1/tenants"

    var options = {
        url: api_end_point,
        headers: {
            "Content-Type": "application/json",
            "x-access-token": access_token
        },
        auth: {
            "user": "admin",
            "pass": "password"
        }
    }

    return new Promise((resolve, reject) => {
        request.get(options, function(error, response, body){
        if(!error && response.statusCode==200){
            var tenants = JSON.parse(body)
            resolve(tenants)

        }
        else{
            reject(error)
        }
    })
})
}

function get_account_access(tenant_id, access_token){
    var api_end_point = nebula_staging_url + "/api/v1/tenants/" + tenant_id + "/access"

    var options = {
        url: api_end_point,
        headers: {"Content-Type": "application/json", "x-access-token": access_token}
    }

    return new Promise((resolve, reject) => {
        request.get(options, function(error, response, body){
        if(!error && response.statusCode==200){
            try{
                var tenants = JSON.parse(body)
                //console.log(tenants)
                resolve(tenants)
            }
            catch(err){
                console.log(err)
            }
        }
        else{
            reject(error)
        }
    })
})
}

// Trying to print something info

get_auth_token().then(token => 
    get_all_tenants(token).then(tenants => {

        var count = 0 
        for(var i=0; j=tenants["data"].length,i<j; i++){

            if(tenants["data"][i]["name"].includes("PERF")){
                var tenant_id = tenants["data"][i]["id"]

                console.log("old_tenant_id", tenant_id)

                get_account_access(tenant_id, token).then(account_access => {

                    console.log("new_tenant_id", tenant_id)

                })
             }

       //break

       count = count + 1
       if(count==3){break}
    }

 }))

```

When I print all the old\_tenant\_id (outside of the get\_account\_access function), it prints the correct ones, However, the new\_tenant\_id only print the last one. the output looks like this:

[![enter image description here](https://i.stack.imgur.com/Z5yd3.png)](https://i.stack.imgur.com/Z5yd3.png)

It seems only the last tenant id go through the get\_account\_access function. I am very new to Nodejs, and I have a feeling to use callback function, However, I have no idea how to implement it in my case. Please be easy on me :)