AngularJS

$q

The Angular $q service provides a promises API.

$q.all

The $q.all API will call all promises contained in the object or array argument passed in and only then will it invoke the .then. In other words after all of the promise calls have completed 'then' do something. This pattern is handy when you want to do something only after a set of operations have all complete, like to load data for a component and then do something with that data.

Basic $q.all example:

private $qService: angular.IQService
...
constructor($q: angular.IQService, ... ) { ... }
...
private loadData() {
  let promises = [
    this.getSomeData(),
    this.getsomeMoreData(),
  ];
  this.$qService.all(promises)
    .then(x => {
      ...
    });
  }
private getSomeData(): angular.IPromise<any> {
  return this.someService.getSomeData().then(x => { this.someData = x; });
}

results matching ""

    No results matching ""