quiquelhappy
quiquelhappy

Reputation: 413

Async function returning existing array as undefined

I have an async function inside my class that does execute like it is supposed, but its return value is undefined when I call it. Executing console.log(array) just before the return line "return array" does work

I've tried setting up the variable in this.array but it is not working either.

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


I'm getting these results:

enter image description here

(from the chrome debug tool)

Upvotes: 1

Views: 528

Answers (1)

Taki
Taki

Reputation: 17654

you have to return something from the async function,

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

Upvotes: 1

Related Questions