我對 Async Await 有基本的了解,但我似乎遺漏了一些東西。
請參閱下面的代碼。我已經放置了 console.log 行來跟蹤執行。我希望每個 console.log 都應該按順序回傳令牌。但“Token1”和“Token3”回傳“未定義”,而“Token2”回傳實際令牌。
問題:
- 我是否正確使用異步等待?
- 我是否正確回傳“credentials.access_token”?
async function GetAuthTokenAsync() {
// Initialize the 2-legged OAuth2 client, set specific scopes and optionally set the `autoRefresh` parameter to true
// if you want the token to auto refresh
var autoRefresh = true; // or false
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(
FORGE_CLIENT_ID,
FORGE_CLIENT_SECRET,
["data:read", "data:write"],
autoRefresh
);
oAuth2TwoLegged.authenticate().then(
function (credentials) {
// The `credentials` object contains an access_token that is being used to call the endpoints.
// In addition, this object is applied globally on the oAuth2TwoLegged client that you should use when calling secure endpoints.
console.log("Token2: ", credentials.access_token); //RETURNS THE TOKEN
return credentials.access_token;
},
function (err) {
console.error(err);
}
);
}
async function Token() {
const token = await GetAuthTokenAsync();
console.log("Token1: ", token); //RETURNS 'undefined'
return token;
}
Token().then(AuthToken => console.log('Token3: ', AuthToken)); //RETURNS 'undefined'
uj5u.com熱心網友回復:
你沒有正確使用它。每個異步函式都回傳一個Promise,但如果你沒有明確的 return 陳述句,它將回傳一個已決議的 promise,其值為undefined. 這就是為什么您將 Token1 視為undefined. 您GetAuthTokenAsync運行呼叫,但它沒有await結果并且不回傳任何內容。
我已經標記了您的代碼,以顯示下面需要進行哪些更改。
async function GetAuthTokenAsync() {
// Initialize the 2-legged OAuth2 client, set specific scopes and optionally set the `autoRefresh` parameter to true
// if you want the token to auto refresh
var autoRefresh = true; // or false
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(
FORGE_CLIENT_ID,
FORGE_CLIENT_SECRET,
["data:read", "data:write"],
autoRefresh
);
return oAuth2TwoLegged.authenticate().then( // <---- Must return a promise
function (credentials) {
// The `credentials` object contains an access_token that is being used to call the endpoints.
// In addition, this object is applied globally on the oAuth2TwoLegged client that you should use when calling secure endpoints.
console.log("Token2: ", credentials.access_token); //RETURNS THE TOKEN
return credentials.access_token;
},
function (err) {
console.error(err);
}
);
}
async function Token() {
const token = await GetAuthTokenAsync();
console.log("Token1: ", token); //RETURNS 'undefined'
return token;
}
Token().then(AuthToken => console.log('Token3: ', AuthToken));
此外,允許在函式.then內部使用async,但它會使其更難閱讀。可以這樣簡化:
async function GetAuthTokenAsync() {
// Initialize the 2-legged OAuth2 client, set specific scopes and optionally set the `autoRefresh` parameter to true
// if you want the token to auto refresh
var autoRefresh = true; // or false
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(
FORGE_CLIENT_ID,
FORGE_CLIENT_SECRET,
["data:read", "data:write"],
autoRefresh
);
const credentials = await oAuth2TwoLegged.authenticate();
console.log("Token2: ", credentials.access_token);
//RETURNS THE TOKEN
return credentials.access_token;
}
如果您真的想捕獲并 console.log 里面的錯誤,GetAuthTokenAsync那么您可以添加一個.catch()函式,但最好讓錯誤通常被拋出堆疊。
uj5u.com熱心網友回復:
你必須這樣做
async function GetAuthTokenAsync() {
// Initialize the 2-legged OAuth2 client, set specific scopes and optionally set the `autoRefresh` parameter to true
// if you want the token to auto refresh
var autoRefresh = true; // or false
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(
FORGE_CLIENT_ID,
FORGE_CLIENT_SECRET,
["data:read", "data:write"],
autoRefresh
);
// you need to await the token before you can use it
const credentials = await oAuth2TwoLegged.authenticate()
// .then(
// function (credentials) {
// // The `credentials` object contains an access_token that is being used to call the endpoints.
// // In addition, this object is applied globally on the oAuth2TwoLegged client that you should use when calling secure endpoints.
// console.log("Token2: ", credentials.access_token); //RETURNS THE TOKEN
// return credentials.access_token;
// },
// function (err) {
// console.error(err);
// }
// );
return credentials.access_token;
}
async function Token() {
const token = await GetAuthTokenAsync();
console.log("Token1: ", token); //RETURNS 'undefined'
return token;
}
Token().then(AuthToken => console.log('Token3: ', AuthToken));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417770.html
標籤:
下一篇:批量Python異步API請求
