1

I have two servince.ts function

getSessions(): Promise<UserSessionInterface[]> {
    return this.sessioAPI.find()
        .toPromise()
        .then(response => response as UserSessionInterface);
}
getUserDetails(id: string): Promise<any> {
    return this.sessioAPI.getUser(id)
        .toPromise()
        .then(response => response.firstName + ' ' + response.lastName)
        .catch(this.handleError);
}

I call them on component.ts like this

getSessions() {
    this._service.getSessions()
        .then(result => {
            this.results = result;
        });
}

getUserDetails(id?: string) {
    return this._service.getUserDetails(id)
        .then(result => {
            console.log('Inside result : ' + result);
            this.userName = result;
        });
} 

getUserName(id: string) {
    return this.getUserDetails(id);
}

I know I can do it with single request inside of component and assign the result to global variable.

But, what I want is to call getUserName function dynamically inside of *ngFor, and return the result of Promise as string directly.

Thanks in advance.

Nimatullah Razmjo
  • 1,831
  • 4
  • 22
  • 39
  • As a comment, since seems you are considering calling function in template, scrap that, because: http://stackoverflow.com/questions/37876533/ngfor-running-an-infinite-loop-in-angular2 – AT82 May 14 '17 at 10:49
  • 1
    `getUserName() | async`? But as pointed out by @AJT_82, that function will be called again on every tick. I've written a blog post on how we're handling async data you might find useful: http://blog.jonrshar.pe/2017/Apr/09/async-angular-data.html – jonrsharpe May 14 '17 at 10:55
  • Exactly, the function is calling again and again on every tick. – Nimatullah Razmjo May 14 '17 at 10:58

0 Answers0