0

So I have a function that uses the refresh token I have to get a new access token and I want to use this function before every call that requires an access token.

The first function refreshAccessToken() makes the request and returns the response including the body that contains the new access token. Now I thought this was enough but I was getting undefined followed by the actual data and doing some digging I found this question:

Promise returns undefined

which led me to returning the value from this function so the promise resolves fully and using it in another, returnAccessToken().

returnAccessToken() is supposed to take the resolved promise value and return the access token but it behaves unexpectedly. The console.log line works like a charm and it logs the correct value everytime but when I return the token and try to use it in the function below it, it is undefined again.

api.js

"use strict"

const request = require("request-promise-native");

refreshAccessToken: async function (credentialsObject) {
    const options = {
        method: "POST",
        uri: <refresh token uri>,
        resolveWithFullResponse: true,
        json: true
    };

    return await request(options);
},

returnAccessToken: function (auth) {
    this.refreshAccessToken(auth)
        .then(function (result) {
            // This logs the result correctly
            console.log(result.body);

            return result.body;
        }, function (err) {
            console.log(err);
        });
},

actionRequiringAccessToken: function (auth) {
    let tokens = this.returnAccessToken(auth);
    // This returns undefined
    console.log(tokens);
}

index.js

Also yes I realize that logging here does nothing as I don't currently return a value I just include it because this is how actionThatRequiresAccessToken() is run in my setup.

"use strict"

const api = require("api");

let auth = {
    // Credentials
};

api.actionRequiringAccessToken(auth)
    .then(function (data)) {
        console.log(data);
    }, function (err) {
        console.log(err);
}
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95
  • the answer provide by @Jonas W. is correct, but I would suggest make `actionRequiringAccessToken` async function and await for `refreshAccessToken` in it, then do wahtever with the result. (you need to wait for `tokens` in both situation anyway) – apple apple Apr 17 '18 at 13:42

3 Answers3

0

Just add return in returnAccessToken method.. you are not returning anything

returnAccessToken: function (auth) { return this.refreshAccessToken(auth)

Sanket Bajoria
  • 748
  • 6
  • 18
0
  return result.body;

That returns the data into the .then chain. So actually your method doesnt return anything. Might add a return here:

 returnAccessToken: function (auth) {
   return this.refreshAccessToken(auth) /*...*0
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

I would suggest make actionRequiringAccessToken async and drop returnAccessToken

actionRequiringAccessToken: async function (auth) {
    let response = await this.returnAccessToken(auth);
    let tokens = response.body //parse the response... whatever returnAccessToken does
    console.log(tokens);
}
apple apple
  • 10,292
  • 2
  • 16
  • 36