[英]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.