在这里我正在调用提供者(http call),它被成功调用,但是 我遇到执行流程问题。我希望输出为
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
它将运行subscribe
函数中的所有内容,因为subscribe
是异步的,其余代码将运行同步。
如果你真的想改变那个流程,只需改变在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);
}