簡體   English   中英

異步函數將現有數組返回為未定義

[英]Async function returning existing array as undefined

我的類中有一個異步函數,它確實像預期的那樣執行,但是當我調用它時它的返回值是未定義的。 在返回行“返回數組”之前執行 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) });


我得到這些結果:

在此處輸入圖片說明

(來自 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) });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM