[英]Async HTTP.get function asynchronously in angular
我必须异步调用 HTTP.get 函数,以便它会等到它的响应没有到来。 在下一行执行之前,它不应该去执行下一行。 我错的地方请帮助我
this.classCheck.checkD().then(present => present ? console.log("Present==true " + present) : console.log(" Present==false " + present));
async checkD() {
let present: boolean = true;
let status : number;
const res = await this.http.get(url,{ headers: header, observe:'response'}).toPromise().then( response => {
console.log(response.status);
status = response.status;
});
if(status >= 400) {
present = false;
}
return present;
}
你需要等待你的承诺。 你或 await 或.then
。 一个是同步的,另一个是异步的。
async checkD() {
let present: boolean = true;
let status : number;
const res = await this.http.get(url,{ headers: header, observe:'response'}).toPromise();
status = res.status;
if(status >= 400) {
present = false;
}
return present;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.