Call function after data retrieved in provider in Ionic2


How do you call a function after data is retrieved from a provider?

I am able to call a function if I pass it in the function in the provider like:
retrieveData(func) {
.map(res => res.json())
.subscribe(data => {

‘func’ is passed like so:

But this does not seem to be the way I am supposed to do it. Right?
Again, I am trying to call a function after the data is retrieved.
So, not just: somedata = this.someProvider.retrieveData());


If func never changes, and you’re certain that it won’t ever need to change in the future, it would seem to make sense to just inline whatever it is in this retrieveData method. If func does change, that tells me from a design perspective that it should be completely separated from retrieveData, because retrieveData's job is to retrieve data, without care about what anybody is going to do with that data.

So I would indeed just eliminate the func param and return the Observable from retrieveData, letting callers handle further transformations and subscriptions. Callers of retrieveData could map or subscribe to the Observable and do whatever they wish, such as calling func() with values that it emits.


Thanks Rapropos,

Could you give a quick example of how to call func() after the caller receives the data?
I am getting a txt file and then running a custom parser over it.

datatext = this.someProvider.retrieveData();

…Now run a function to parse that string


It sort of depends on whether func is intended to transform the data (IOW, you care about what func returns) or just do something to it (func probably returns void). Let’s define retrieveData like so:

retrieveData(): Observable<Object> {
  return this.http.get('./something.json')
    .map(res => res.json());

If func is going to turn that Object into a Foo, for example,

function func(raw:Object): Foo {
  return this.fooify(raw);

retrieveFoo(): Observable<Foo> {
  return provider.retrieveData().map(func);

If func is just going to do something to the Object?

function func(obj:Object): void {
  // do something

  .subscribe((data) => {


Thanks for those options, a lot.

I got it working.
Got stuck for a while until I realized that my problems were stemming from calling the provider from the variable I wanted the retrieved data to become.

eg: this.someData= this.someProvider.retrieveData();

The following works great - Thanks again!
.subscribe((data) => {