Code
async function getStudents() {
var reqObj = new RequestHandler("/students/all/");
let { students } = await reqObj.sendRequest();
console.log(students, students.length);
return students;
}
export default function Students(props) {
var students = getStudents();
return (
<main className="content-section">
<h1>Students</h1>
<span className="help-text">
You have {`${students.length}`} students.
</span>
</main>
);
}
...
export class RequestHandler {
// Base class for handling requests
constructor(url, method = "GET", contentType = "application/json") {
this.url = url;
this.method = method;
this.contentType = contentType;
this.request_conf = {
method: this.method,
url: this.url,
headers: {
"Content-Type": this.contentType,
"X-CSRFToken": cookies.get("csrftoken"),
},
credentials: "same-origin",
};
}
async sendRequest() {
const response = await axios(this.request_conf);
var data = isResponseOK(response);
return data;
}
}
The console.log(students, students.length); within the getStudents() function gives me the output just as I expect: [] 0, but despite trying to make it an asynchronous function and all, the Students component still get undefined as value back.
What would be the easiest way to get the same value read within the Students component?
DISCLAIMER
I am a beginner at JavaScript and especially Promises, so if there's something really simple and/or obvious that I am missing here, please tell me so and don't be toxic about it :)