【问题标题】:Async function returning existing array as undefined异步函数将现有数组返回为未定义
【发布时间】:2019-08-16 21:53:43
【问题描述】:

我的类中有一个异步函数,它确实按预期执行,但是当我调用它时它的返回值是未定义的。在返回行“返回数组”之前执行 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 调试工具)

【问题讨论】:

    标签: javascript


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2018-03-12
      • 1970-01-01
      相关资源
      最近更新 更多