Directly on the code:
const request = require('request-promise-native');
var config = require('.. /config.js');
async function getAccessToken(){
var raw = new Buffer(config.username + ":" + config.password);
const accessToken = await request({
method: 'GET'.headers: {
'Authorization': 'Basic ' + raw.toString('base64')},url: config.ACCESS_TOKEN,
json: false
});
return accessToken;
}
module.exports = getAccessToken;
Copy the code
Consumption Code:
accessTokenService().then(function(oResult){
console.log(oResult);
});
Copy the code
After stepping, the debugger skips the getAccessToken function at line 7. Line 16 also skips the getAccessToken function without stepping. After a few seconds, the breakpoint is triggered directly from the return keyword on line 16.
At this point, the accessToken variable contains a pure Access Token, not a wrapped promise object.
However, the entire async function ends up returning a Promise object that contains the access token obtained using the then method provided by the Promise object:
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: