2

Hello i have this function for get user access token

chrome.identity.getAuthToken({ 'interactive': false }, function(token){ 
   ... 
});

but i want to use access token as variable for example:

var token = someFunction();

there is some method to get this? or use this token out of callback function?

1 Answers1

1

chrome.identify.getAuthToken is an asynchronous function, so it's not possible to assign a variable token to the return value. When the network request has completed successfully, the function will execute the callback function, passing in the token as the first argument. You can only guarantee you have a token value when it executes the callback.

Note that the callback function doesn't need to be anonymous as used above. You can separate out the logic as follows:

function authoriseUser() {
    chrome.identity.getAuthToken({ 'interactive': false }, processToken);

    function processToken(token) {
      // ...
    }
}
Gideon Pyzer
  • 22,610
  • 7
  • 62
  • 68