TS Code:
var APIres: any;
export class Component implements OnInit {
async getInfo(){
await this.apicall();
console.log('API response', APIres)
}
async apicall(){
var request = new XMLHttpRequest();
request.open('POST', 'URL', true);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', 'Bearer ' + accessToken);
request.onreadystatechange = async function () {
console.log('Got Values')
await res(JSON.parse(this.responseText));
}
request.send(content);
}
}
async function res(x){console.log('out');APIres = x} //Outside of export
Output:
API response undefined
Got Values
out
Desire Output:
Got Values
out
API response res