为什么我调用 Ghash 函数时会收到 [Promise]?
当我调用 Gash 函数时,我得到了一个承诺 我需要接收这个承诺的结果,它是一个哈希值,如果没有这个哈希值,
async function Ghash(name){
let res = await fetch(`https://account.imperiomc.click/${name}.json?`+new Date().getTime()), json = await res.json(), hash, skin;
if (json.skins.slim != null){
hash = await json.skins.slim
skin = "Alex"
} else {
hash = await json.skins.default
skin = "Steve"
}
console.log("Skin model is "+skin)
return hash
}
function populateAuthAccounts(){
const authAccounts = ConfigManager.getAuthAccounts()
const authKeys = Object.keys(authAccounts)
if(authKeys.length === 0){
return
}
const selectedUUID = ConfigManager.getSelectedAccount().uuid
let authAccountStr = ''
authKeys.map((val) => {
const acc = authAccounts[val]
const hash = Ghash(acc.displayName)
const settingsCurrentAccounts = document.getElementById('settingsCurrentAccounts')
let url = `https://account.imperiomc.click/preview/hash/${hash}?`+new Date().getTime();
authAccountStr += `<div class="settingsAuthAccount" uuid="${acc.uuid}">
<div class="settingsAuthAccountLeft">
<img class="settingsAuthAccountImage" alt="${acc.displayName}" src="${url}">
</div>
<div class="settingsAuthAccountRight">
<div class="settingsAuthAccountDetails">
<div class="settingsAuthAccountDetailPane">
<div class="settingsAuthAccountDetailTitle">Usuario</div>
<div class="settingsAuthAccountDetailValue">${acc.displayName}</div>
</div>
<div class="settingsAuthAccountDetailPane">
<div class="settingsAuthAccountDetailTitle">UUID</div>
<div class="settingsAuthAccountDetailValue">${acc.uuid}</div>
</div>
</div>
<div class="settingsAuthAccountActions">
<button class="settingsAuthAccountSelect" ${selectedUUID === acc.uuid ? 'selected>Selected Account ✔' : '>Selecciona una cuenta'}</button>
<div class="settingsAuthAccountWrapper">
<button class="settingsAuthAccountLogOut">Cerrar sesión</button>
</div>
</div>
</div>
</div>`
settingsCurrentAccounts.innerHTML = authAccountStr
})
}
如果我将 authKeys.map((val) 的所有内容放在 .then 中,对于 示例(点击我) 功能 不适用于按钮,但哈希给了我我正在寻找的
结果将 Ghash 导出到哈希(没有任何 .then 或异步代码) 这与 为什么是[...] 而不是一个值? 如果我这样做,我就会遇到新问题,请参阅“如果我将 [...] 放入 .then...”版主(不要关闭),
如果有人可以的话告诉我我怎样才能修复它...我会非常感激你
When I call the Ghash function, I get a promise
I need to receive the result of this promise which is a hash, without that hash I cannot get the image I am looking for
async function Ghash(name){
let res = await fetch(`https://account.imperiomc.click/${name}.json?`+new Date().getTime()), json = await res.json(), hash, skin;
if (json.skins.slim != null){
hash = await json.skins.slim
skin = "Alex"
} else {
hash = await json.skins.default
skin = "Steve"
}
console.log("Skin model is "+skin)
return hash
}
function populateAuthAccounts(){
const authAccounts = ConfigManager.getAuthAccounts()
const authKeys = Object.keys(authAccounts)
if(authKeys.length === 0){
return
}
const selectedUUID = ConfigManager.getSelectedAccount().uuid
let authAccountStr = ''
authKeys.map((val) => {
const acc = authAccounts[val]
const hash = Ghash(acc.displayName)
const settingsCurrentAccounts = document.getElementById('settingsCurrentAccounts')
let url = `https://account.imperiomc.click/preview/hash/${hash}?`+new Date().getTime();
authAccountStr += `<div class="settingsAuthAccount" uuid="${acc.uuid}">
<div class="settingsAuthAccountLeft">
<img class="settingsAuthAccountImage" alt="${acc.displayName}" src="${url}">
</div>
<div class="settingsAuthAccountRight">
<div class="settingsAuthAccountDetails">
<div class="settingsAuthAccountDetailPane">
<div class="settingsAuthAccountDetailTitle">Usuario</div>
<div class="settingsAuthAccountDetailValue">${acc.displayName}</div>
</div>
<div class="settingsAuthAccountDetailPane">
<div class="settingsAuthAccountDetailTitle">UUID</div>
<div class="settingsAuthAccountDetailValue">${acc.uuid}</div>
</div>
</div>
<div class="settingsAuthAccountActions">
<button class="settingsAuthAccountSelect" ${selectedUUID === acc.uuid ? 'selected>Selected Account ✔' : '>Selecciona una cuenta'}</button>
<div class="settingsAuthAccountWrapper">
<button class="settingsAuthAccountLogOut">Cerrar sesión</button>
</div>
</div>
</div>
</div>`
settingsCurrentAccounts.innerHTML = authAccountStr
})
}
if I put all the content of authKeys.map((val) inside .then, for example (click me)
the functions doesnt work with the buttons but the hash gives me the result I am looking for
i need to export the Ghash to hash (without any .then or async the code)
that this is not related to Why is [...] instead of a value? if i do that, i have new issue, see the "if I put [...] inside .then..." moderators (dont close)
if anyone can tellme how i can fix it... I would be very grateful to you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你应该放置
await
,因为Gash
函数是异步的,你需要等待响应。Maybe you should put
await
, because theGash
function is asynchronous, you need to wait for the response.