I'm new to web development and have been using a node.js web server running express as a backend for an app I'm working on for my school. One of the things this server needs to do is to query the school's database, receive information, and return that to the user. While I've gotten it to work, I'm receiving a delay of almost 2 seconds from the start to the end of the request. This is because the server must make 2 requests to get the information it needs. What I'm trying to figure out is how to make both of them run asynchronously and preforming tasks once the information is received. Attached is some demo code showing what I've got so far.
axios
.get(
`https://schoolserver.com/classes`,
config
)
.then((res) => {
classes = res.data.value;
axios
.get(
`https://schoolserver.com/meetings`,
config
)
.then((res) => {
meetings = res.data.value;
}
response.send(classes, meetings);
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
As you can see I have 2 axios requests nested within each other which creates unwanted delay. I want a solution where both requests are done independently and then code is executed once the data is received. If there are any solutions please reach out.