Function node return doesnt await?

Hello I have a script function that need to return a request object.
But the return is not returning the res object.

I have tried moving the return inside the async await but it never executes…

What I’m doing wrong?

Any ideas? @MutedJam @jan

((I have to remove the API keys as I cant regen them))

const request = require('request')

const OAuth = require('oauth-1.0a')

const crypto = require('crypto') // depenency package for OAuth-1.0a

let res;

function generateToken() {

    return new Promise((resolve, reject) => {

// Token request function

    // #1 Initialize OAuth with your HERE OAuth credentials from the credentials file that you downloaded above

    const oauth = OAuth({

        consumer: {

            key: 'xxxx', //Access key

            secret: 'xx-xxx', //Secret key

        },

        signature_method: 'HMAC-SHA256',

        hash_function(base_string, key) {

            return crypto

                .createHmac('sha256', key)

                .update(base_string)

                .digest('base64')

        },

    });

    // #2 Building the request object.

    const request_data = {

        url: 'https://account.api.here.com/oauth2/token',

        method: 'POST',

        data: { grant_type: 'client_credentials' },

    };

    // #3 Sending the request to get the access token

    request(

        {

            url: request_data.url,

            method: request_data.method,

            form: request_data.data,

            headers: oauth.toHeader(oauth.authorize(request_data)),

        },

        function (error, response, body) {

            if (response.statusCode == 200) {

                result = JSON.parse(response.body);

                res = result;

                resolve();

            }

           

            else {

                 reject('Error: Something went wrong!');      

            }

        }

    );

// Calling this function to get the access token

    })

 

};

async function init(){

    await generateToken();

    console.log(res);

}

init();

return [{

    json: {

        arr: res

    }

}];

Thanks

Did you try to add an await in front of init()?

Oh my god, I can’t believe I did not add await in front of init :man_facepalming: :man_facepalming:

Thanks for catching the error

Great to hear that it helped! Have fun!