gpt4 book ai didi

javascript - 异步函数将现有数组返回为未定义

转载 作者:行者123 更新时间:2023-11-30 19:20:30 27 4
gpt4 key购买 nike

我的类中有一个异步函数,它确实按预期执行,但是当我调用它时它的返回值是未定义的。在返回行“return array”起作用之前执行 console.log(array)

我已经尝试在 this.array 中设置变量,但它也不起作用。

class Core {
constructor(key) {
this.key = key;
}
async getSessions() {
var finalresponse = []
try {
// wait for response
await fetch("https://api.purecore.io/rest/1/session/get/?key=" + this.key, { method: "GET" }).then(function (response) {
return response.json();
}).then(function (jsonresponse) {
// looks for errors
if (jsonresponse.error != null) {
throw new Error("PureCore returned an error: " + jsonresponse.error + " -> " + jsonresponse.msg)
} else {
// adds the sessions to the response
jsonresponse.forEach(player => {
finalresponse.push(new CoreSession(player["mojang_username"], player["mojang_uuid"], player["core_id"], player["verified"]))
});
console.log(finalresponse) // returns array list
return finalresponse; // returns undefined
}
});
} catch (e) {
throw new Error("Error while getting the response for 'https://api.purecore.io/rest/1/session/get/?key=" + this.key + "' -> " + e.message)
}
}
}

class CoreSession {
constructor(username, uuid, core_uuid, verified) {
this.username = username;
this.uuid = uuid;
this.core_uuid = core_uuid;
this.verified = verified;
}
}

// testing:
sessions = new Core("731b59d106ea5acd0a385958d8e0f18b4b74b741f28f6efa43ed4a273a42d6f9").getSessions().then(function (value) {
console.log(value)
}, function (reason) {
console.log(reason)
});


我得到这些结果:

enter image description here

(来自 chrome 调试工具)

最佳答案

你必须从异步函数返回一些东西,

// wait for response
return await fetch("https://api.purecore.io/rest/1/session/get/?key=" + this.key, { method: "GET" }).then(function (response) {

class Core {
constructor(key) {
this.key = key;
}
async getSessions() {
var finalresponse = []
try {
// wait for response
return await fetch("https://api.purecore.io/rest/1/session/get/?key=" + this.key, { method: "GET" }).then(function (response) {
return response.json();
}).then(function (jsonresponse) {
// looks for errors
if (jsonresponse.error != null) {
throw new Error("PureCore returned an error: " + jsonresponse.error + " -> " + jsonresponse.msg)
} else {
// adds the sessions to the response
jsonresponse.forEach(player => {
finalresponse.push(new CoreSession(player["mojang_username"], player["mojang_uuid"], player["core_id"], player["verified"]))
});
console.log(finalresponse) // returns array list
return finalresponse; // returns undefined
}
});
} catch (e) {
throw new Error("Error while getting the response for 'https://api.purecore.io/rest/1/session/get/?key=" + this.key + "' -> " + e.message)
}
}
}

class CoreSession {
constructor(username, uuid, core_uuid, verified) {
this.username = username;
this.uuid = uuid;
this.core_uuid = core_uuid;
this.verified = verified;
}
}

// testing:
sessions = new Core("731b59d106ea5acd0a385958d8e0f18b4b74b741f28f6efa43ed4a273a42d6f9").getSessions().then(function (value) {
console.log(value)
}, function (reason) {
console.log(reason)
});

关于javascript - 异步函数将现有数组返回为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531561/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com