I am executing the following script and callbacks from XHR request increasing gradually(1, 2, 3.....etc) but network call happens only onetime.
class Service {
profiles = [] //assume array contains objetcs
xhr = new XMLHttpRequest();
token = null;
constructor(token) {
this.xhr = new XMLHttpRequest();
this.token = token
}
setInterval(res => {this.doFunc()}, 10000);
doFunc() {
if (this.profiles.length > 3) {
this.doChoice(this.profiles[0]).then(response => {
console.log(response); //printing only onetime
});
}
}
async doChoice(profile) {
return await new Promise(resolve => {
this.like(profile.id, response => {
//code below is excuting gradually with interval method
console.log('Liked');
console.log(profile);
this.profiles.splice(this.profiles.indexOf(profile), 1);
resolve(JSON.parse(response));
});
})
}
like(id, subscribe = {res: ''}) {
let url = 'https://someurl/' + id;
this.xhr.open("GET", url);
this.xhr.setRequestHeader("x-auth-token", this.token);
this.xhr.addEventListener("readystatechange", function () {
//code below is excuting gradually with interval method
if (this.readyState === 4 && this.status === 200) {
if (this.responseText) {
console.log(this.responseText);
subscribe(this.responseText);
}
}
});
this.xhr.send();
}
}
If someone can explain to me what I am doing wrong here that would be awesome!