1

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)

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.