Subscribing to Watched by Unsynchronized Calls [Angular2]

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

+5
source share
1 answer

Not sure where your problem is, but it is more understandable and may work.

 getData(args, parentKey?):any { this.apiService.get(args) .map(response => { data: response['data'], status: ($.isEmptyObject(response) ? false : true) }) .do(() => console.log("finished")) .catch(error => console.log("error : ", error)); } 
0
source

Source: https://habr.com/ru/post/1267118/


All Articles