Letâs back up here. When dealing with asynchronous programming, there are, generally speaking, two kinds of functions:
Type 1: they do what theyâre going to do when they do it, and everybody else just has to deal with the fact that they donât know when itâs been done. They must return void.
Type 2: they return a future and anybody who calls them must only attempt to access the result once the future has resolved. Almost always, the first word you type in these functions should be âreturnâ.
You can go either way here, but you donât want to be instantiating needless promises.
Option 1:
getPartners(): void {
this.http.get('http://www.cinemairis.it/wp-json/wp/v2/partners').finally(() => {
// we do this here so that the loading gets dismissed when errors happen as well
this.loading.dismiss();
}).subscribe((rsp) => {
this.items = rsp.json().results;
}, (err) => {
});
}
You should probably initialize this.items to a reasonable default (such as an empty array) if you are using it the way people typically do (such as backing an ngFor), or you will get template errors.
getData() {
this.httpClient.get('url').subscribe(
data => {
this.incomingdata = data as any[]; // FILL THE ARRAY WITH DATA.
},
);
return this.incomingdata; //how can I return above data here
}
i have also same problem ,how can i use" this.incomingdata" out side of the function
getData()
.then(data=>{
this.listitems = data[âdataâ]
// or you can use this.listitems = data.data <â but error when compile t0 ionic cordova run android
})
xmarkSolicitud(idTran:string, stagePos:string){
let urlEndPoint=this.urlApi+idTran+'/stages/'+stagePos+'/mark';
//Creating the promise
return new Promise((resolve, reject) => {
this.http.post(urlEndPoint,'',{headers:this.headers}).subscribe(
res=>{
console.log('Mark Transaction OK, idTransac='+idTran);
resolve(res); //value to return out of the promise
},(err: HttpErrorResponse)=>{
reject(err); //returning Error
console.log(err.error);
console.log(err.name);
console.log(err.message);
console.log(err.status);
}
);
});//end Promise
}//end xmarktype or paste code here
2- Using the data:
this.medService.xmarkSolicitud('617163c535e03f82a19daeaf73e5547c','0').then(resp=>{
console.log(resp);
//make your stuff with the response (resp)
}, error=>{
//manage error
console.log(error);
});