I am using an API where the response is coming inside addEventListener function which I want to pass to a function after getting the response received. I am tying to achieve something as follows:
getToken() {
var data = JSON.stringify({
"app_key": "my_app_key",
"app_secret": "my_app_secret"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", async function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
await val = this.responseText;
this.recieveApiResponse(val);
}
});
xhr.open("POST", "https://tokenized.sandbox........../grant");
xhr.setRequestHeader("username", "my_username");
xhr.setRequestHeader("password", "my_password");
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(data);
}
recieveApiResponse(resp) {
console.log(resp)
}