I have a getData () method in my service.ts as shown below. This returns the Observable. Another ApiService also has a get () method, which also returns an Observable.
getData(args, parentKey?):any { return new Observable(observer => { this.apiService.get(args).subscribe( response => { observer.next({ data: response['data'], status: ($.isEmptyObject(response) ? false : true) }); }, error => console.log("error : ", error), () => console.log("finished") ); }); }
I am trying to reuse this getData () method in asynchronous API calls that must be made from my component. See below:
this.myService.getData({ url: 'apiUrl1', method: 'Method1', parameters: {} }).subscribe(response => { if (response['status']) { console.log('Processed apiUrl1'); } }); this.myService.getData({ url: 'apiUrl2', method: 'Method2', parameters: {} }).subscribe(response => { if (response['status']) { console.log('Processed apiUrl2'); } }); this.myService.getData({ url: 'apiUrl3', method: 'Method3', parameters: {} }).subscribe(response => { if (response['status']) { console.log('Processed apiUrl3'); } });
Error: But what happens is that the getData-observable always subscribes to apiUrl3.
I need each getData () to be signed individually. Can someone please help me?
Expected Result:
Processed apiUrl1
Processed apiUrl2
Processed apiUrl3
source share