I am trying to perform a GET
call in typescript to various services and capture a value from their json response which will be passed in as a param in another function.
Through some online searching, I bumped into axios but am having a hard time wrapping my head around asynchronous apis and how to handle promises. Here is the code, that returns 'undefined' as an output.
function getVersion(url: string) {
let version;
let res = await axios.get(url)
.then((response: { data: { buildInfo: { details: { version: any; }; }; }; }) => {
version = response.data.buildInfo.details.version;
return version;
})
.catch((error: any) => {
console.log(error);
});
return version;
}
I've also tried async/await but have trouble with unhandled promises. I don't need to use axios, any library will do as long as I can access the response to use in consequent functions.