异步函数返回未定义的现有数组

时间:2019-08-16 21:53:43

标签: javascript

我的类中有一个异步函数,该函数确实像预期的那样执行,但是当我调用它时,其返回值是不确定的。在返回行“ 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调试工具)

1 个答案:

答案 0 :(得分:1)

您必须从async函数返回一些内容

// 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)
});