I have this code in my JavaScript HTTP client class:
export class HttpClient {
constructor(url) {
this.url = url;
this.xhr = new XMLHttpRequest();
this.xhr.onloadend = (event) => {
console.log('ONLOADEND', this.xhr.response);
return this.xhr.response;
}
}
get(async, header) {
return new Promise((resolve, reject) => {
this.xhr.open('GET', this.url, async);
this.xhr.send();
console.log('RESPONSE', this.xhr.response);
resolve(this.xhr.response);
});
}
}
In this code the get()
method finished before the this.xhr.onloadend
finished in the constructor.
In my class where I use this HttpClient class I use like this:
this.httpClient.get(true, this.header).then((result) => {
// result is never get the xhr.response here
});
How can I return / resolve with the response after datas loaded?