I am trying to make a GET Request to an API and want to store the data it returns in another variable but Javascript doesn't wait for the request to be complete and log my variable as Undefined
app.get("/", function (req, res) {
const url = "https://animechan.vercel.app/api/random";
let str = "";
let quote;
https.get(url,(resposne)=>{
resposne.on("data",(data)=>{
str+=data;
});
resposne.on("end",()=>{
const info=JSON.parse(str);
quote=info.quote;
});
});
console.log(quote);
res.render("header", {
quote: quote
});
});
I would be glad if someone can tell me how to solve this problem and where can I study about it more as I am a beginner to javascript.