Angular + ES6 Promise

It would be really nice if we could use native ECMAScript 2015 Promises with Angular instead of $q service that is provided from box to be close to pure JavaScript:

//somewhere inside component controller
let promise = new Promise((resolve) => setTimeout(() => resolve('resolved'), 2000));
promise.then(x => this.x = x);

But in this case we will have to run digest manually for each resolve(to synchronise view and model):

let promise = new Promise((resolve) => setTimeout(() => resolve('resolved'), 2000));
promise.then(x => {
  $scope.apply();
  this.x = x;
});

But what if we hack the Promise and intercept our digest call there:

class SubPromise extends Promise {
  constructor(executor) {
    super(function(_resolve, _reject) {
      var resolve = (data) => {
        var res = _resolve(data);
        angular.element(document.body).injector().get('$rootScope').$apply();
        return res;
      }
      return executor(resolve, _reject);
    });
  }
}

now we just need to overwrite standard Promise:

window.Promise = SubPromise;

to keep it simple to cover for unit-tests you can also wrap it into an angular factory:

factory('Promise', () => Promise);

Here you can play with the code.

Discussion on stackoverflow about customising ES6 Promise.

!Attention: This experiment was made just for learning purposes and it should not be applied for the real projects.