Reputation: 97
I'm trying to get the result of async
function on ngOnInit
.
On .ts file, I add this function:
async load()
{
this.picaes = await this.userService.getAffectedExpertisesPromiseSE(25);
console.log("-Inside load--------------" + this.picaes);
return this.picaes;
}
On ngOnInit, I call this async
funtion:
console.log("-Outside load--------------" + this.load());
On the file service.user.ts, I made this call:
async getAffectedExpertisesPromiseSE(id: number): Promise<any>
{
var url= `${this.baseUrl}/users/expertisesObj/${id}`;
var result= await (await fetch(url)).text();
return result;
}
That produces:
-Outside load--------------[object Promise]
-Inside load--------------[{"id":1,"name":"Angular","selected":true},{"id":2,"name":"SpringBoot","selected":true},{"id":3,"name":"Primefaces","selected":true}]
So my problem is that I can't get the result on the console Outside load.
Could you please tell me what I missed ?. Big thanks.
Upvotes: 2
Views: 432
Reputation: 1248
Why not trying using XMLHttpRequest
instead of async
var request = new XMLHttpRequest();
request.open('GET', 'http://localhost:6227/api/auth/users', false);
request.send(null);
You can use this thread, this may help you maintaing synchrnous call.
HTH.
Upvotes: 2
Reputation: 1310
Async function returns a promise not your expected output. Therefore you will not be getting the values on this.load(). Instead change your code to:
this.load().then((data) => {
console.log('Outside load--------------' +data);
})
Upvotes: 4