0

I want to create a function that return an object from service that uses external API. I am not able to make it work.

GetMatchinfo (matchid : number  ) : Match {

    let aq :Match;

    this.matchService.GetMatch(matchid).subscribe(
        (Matchinfodata: Match)=>{     
            aq=Matchinfodata;
         }
    ); 

    return aq;

}
MonkeyScript
  • 4,776
  • 1
  • 11
  • 28
Mehdi Kr
  • 37
  • 5

1 Answers1

1

If you are calling the function from template, just move the position of return call to subscription block. Or if you are using the GetMatchInfo() inside some other function, it's better to use observable and subject.

GetMatchInfo (matchid : number  ) : Observable<Match> {

    let aq = new Subject<Match>();

    this.matchService.GetMatch(matchid).subscribe(
        (Matchinfodata: Match)=>{     
            aq.next(Matchinfodata);
         }
    ); 

    return aq.asObservable();

}

And subscribe to GetMatchInfo() function somewhere you require.

this.GetMatchInfo(id).subscribe(
    (result) => {
        console.log(result)
    }
);
Ralpharoo
  • 789
  • 9
  • 21
MonkeyScript
  • 4,776
  • 1
  • 11
  • 28