[英]Export variable defined by an async function - React Native
我正在嘗試將獲取的結果存儲在 const LANGUAGE 中。 但它不起作用,問題是異步 function。 語言等於:
{"_U": 0, "_V": 0, "_W": null, "_X": null}
雖然它應該包含那里可訪問的數據。 我的代碼是:
async function get_language() {
const response = await fetch("https://harmony-project.xyz/app/get-language.php");
return await response.json();
}
export const LANGUAGE = get_language();
console.log(LANGUAGE);
誰能告訴我應該如何存儲結果?
{"_U": 0, "_V": 0, "_W": null, "_X": null}
這意味着您有一個未解決的 promise。 這是因為當您使用 async 關鍵字聲明 function 時,function 返回 promise。 試試這個實現。
var url = "https://harmony-project.xyz/app/get-language.php";
async function get_language() {
const response = await fetch(url);
return await response.json();
}
get_language().then((languages) => {
console.log(languages);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.