I'm trying to call the Wikipedia API to retrieve a random title of a page. I want to save the title of the page in a variable for later use but I can not find a way to resolve the promise. I keep getting ZoneAwarePromise returned. I'm very new to promises and typescript in general, is there any way to solve this?
I've tried adding await on the promises, but I still get returned promises.
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);});
}
I expect the output to just be the title pulled, I can only seem to console.log the title but I want to save it to a variable for use in other functions.