这里我正在调用提供者(http调用),它被成功调用但是 我遇到了执行流问题。我希望输出为
4.3274
hi
but its printing
hi
4.3274
<ion-buttons end>
<button ion-button (click)="showcalculation()">Calculate</button>
</ion-buttons>
showcalculation(){
this.quoteServe.calSunShine().subscribe(data=>{
console.log(data);
})
console.log("hi");
}
**provider method:**
calSunShine(){
const body=new URLSearchParams();
const header= new Headers();[enter image description here][1]
header.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(this.base_url+"restInsertQuote/getsunshine", body.toString(),{headers : header}).map(response => response.json());
}
[1]: https://i.stack.imgur.com/zHyQ3.png
答案 0 :(得分:1)
它会在之后运行subscribe
函数中的所有内容,因为subscribe
是 async ,其余代码将运行同步
如果您真的想要更改该流程,只需更改要在subscribe
内部调用的代码,例如:
//Somewhere in your class
let sunshine: any;
showcalculation(){
this.quoteServe.calSunShine().subscribe(data=>{
//run this before
console.log("hi");
//then
console.log(data);
// Extended example
this.sunshine = data;
// Let's use sunshine for something
// This will run after and will print server data.
this.doSomethingWithSunshine();
});
//This will run first and will print undefined.
this.doSomethingWithSunshine();
}
doSomethingWithSunshine(): void {
console.log(this.sunshine);
}