[英]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.