Is there a way to use TypeScript to call the Wikipedia API for retrieving a random page title and save it in a variable for later use? I am struggling with resolving promises as I keep getting ZoneAwarePromise returned. I'm new to both promises and TypeScript, so any guidance on how to resolve this issue would be greatly appreciated.
I have attempted to add await to my promises, but I continue to encounter returned promises instead of the actual data.
async getPage() : string {
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
generator: "random",
rnnamespace: "0",
format: "json",
list: "random",
rnlimit: "1"
};
url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});
return await fetch(url)
.then (response => response.json())
.then(data => data.query.random[0].title)
.catch(function(error){console.log(error);});
}
My goal is to extract the title from the retrieved data and store it in a variable for future use within other functions.