1

I want to use my API asynchronously. I get some data from my API and i need to display them.

Here an example :

home.ts

async ngOnInit()
{
    console.log("NgInit 01");
    this.data = await this.api.getMe();
    console.log("NgInit 02");
    this.canDisplayData = true;
}

home.html

<div *ngif="canDisplayData">
   myData : {{ data }}
</div>

service_api.ts

async getMe() {
    let result: any;

    await this.http.get('http://dummy.restapiexample.com/api/v1/employees')
        .toPromise()
        .then(res => { result = res; })
        .catch(error => { console.log(error); });

    return result;
  }

this works, but i'm not proud at all of it. I'm sure i can do something better. Without use async and await.

Thank you for teaching me life, of angular/ionic :D

1
  • Try using observable / subscribe Commented Dec 11, 2018 at 9:43

3 Answers 3

1

async/awit work on promise only so you need to convert observable to promise

service_api.ts

getMe() {
    return this.http.get('http://dummy.restapiexample.com/api/v1/employees')
  }

home.ts

async ngOnInit()
{
    console.log("NgInit 01");
    this.data = await this.api.getMe().toPromise();
    console.log("NgInit 02");
    this.canDisplayData = true;
}

another way is to use async pipe 🚀🚀

async/awit work on promise only so you need to convert observable to promise

service_api.ts

getMe() {
    return this.http.get('http://dummy.restapiexample.com/api/v1/employees');
  }

home.ts

ngOnInit()
{
    console.log("NgInit 01");
    this.data$ = this.api.getMe();
    console.log("NgInit 02");
}

tenplate

<div >
   myData : {{ data$ | async}}
</div>
Sign up to request clarification or add additional context in comments.

7 Comments

super minor note, I wouldn't call it this.data anymore but more like this.dataPromise. You probably would do the same but people who read this would probably need to take note. (I didn't downvote before)
@misha130 I don't get your point is there any wrong with my answer so I can edit
this.dataPromise = this.api.getMe(); {{ dataPromise | async }} <- I meant like this, because its no longer the data but rather the promise of the data
I'd recommend learning the power of observables instead of converting to promises.
@ShamPooSham I never use toPromise becuase I don't use async/awit 😉
|
0

I personally don't recommend to use async and await with angular when we have observable and subscribe of rxJs, just convert your code like this save extra variables too -

ngOnInit() {
  this.api.getMe()
    .subscribe(res => {
      this.data = res;
    },
    error => {
      console.log(error);
    });
}

<div>
   myData : {{ data }}
</div>

getMe() {    
    return this.http.get('http://dummy.restapiexample.com/api/v1/employees')
      .map(res => {
        return res;
      });
  }

9 Comments

1) let result: any in getMe() isn't needed anymore. 2) In Angular 6+, you need to use pipe on the observable and use the operator map inside of it.
@misha130 HttpClient will complete itself, no need to unsubscribe.
@misha130 totally agree with your point, As I replicate only question part so forogot to add unsubscribe, also I answered assuming OP is using httpModule not httpClient.
@PardeepJain You're using the old rxjs syntax, take a look at the examples here: learnrxjs.io/operators/transformation/map.html
@misha130 That's true, I'm not familiar with ionic. Sure, it's not a bad point
|
0

You can go with below one, async and await can slow your app.

Home.ts

 ngOnInit()
        {
            this.api.getMe().subscribe((data)=>{
               this.canDisplayData = true;
               this.data = data;
            }, (err) => {
               console.log(err);
            });
        }

Home.html

<div *ngif="canDisplayData">
   myData : {{ data }}
</div>

service_api.ts

getMe() {
    return this.http.get('http://dummy.restapiexample.com/api/v1/employees');
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.