I am trying to run this function sequentially. The output should be:
latitude length,
lat length A,
lat length B
But the output I am getting is:
latitude length,
lat length B,
lat length A
I want to process this.adminService.getUserList().subscribe(res => {}
first but it is getting processed after the completion of makeAnAPICall()
function.
My code is below:
async makeAnAPICall():void {
console.log("latitude length",this.latitude.length);
this.latitude = [];
this.longitude = [];
delay(2000);
await this.adminService.getUserList().subscribe(res => {
for(let i =0; i < res.length; i++) {
this.latitude.push(res[i].latitude);
this.longitude.push(res[i].longitude);
};
console.log("lat length A",this.latitude.length)
});
console.log("lat length B ",this.latitude.length);
}
You need to convert your observable to promise first.
async makeAnAPICall():void {
console.log("latitude length",this.latitude.length);
this.latitude = [];
this.longitude = [];
delay(2000);
const res = await this.adminService.getUserList().first().toPromise();
for(let i =0; i < res.length; i++) {
this.latitude.push(res[i].latitude);
this.longitude.push(res[i].longitude);
};
console.log("lat length A",this.latitude.length)
console.log("lat length B ",this.latitude.length);
}