1

I am trying to queue or create a callback function after fetching data from service, but the function seems to be called asynchronously. I'd like the function to be called after the data is fetched as the data in the Observable is required for the function. So far this doesn't work as the function is called before all the data has been assigned to this.items:

this._itemsService.getItems().subscribe(items => this.items = items, err => {}, callThisFunctionAfter());

Is there a way to turn the function into a callback or queue it like a promise?

skyscript
  • 175
  • 2
  • 12

1 Answers1

1

You need to make it a closure not a function call.

this._itemsService.getItems().subscribe(items => this.items = items, err => {}, () => callThisFunctionAfter());

Without () => the function is called and the result is passed as callback.

This is the reason callThisFunctionAfter() is executed before subscribe() instead of when the observable was closed.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567