0

So I have some code in a service that looks like this:

getData(params) {
    // ... config, url
    return this.$http.get(url, config)
        .then(result => {...})
        .catch((e) => {...})
}

And I use it in a component like so:

ctrl.service.getData(params).then(result => {...}).catch((e) => {...})...;

The thing is, I want to be able the cancel this promise, something that can be achieved fairly easy using Bluebird. But if I wrap my $http call in a Promise.resolve($http...) then I'm missing out on the digest cycle triggered by the $http call and my UI doesn't get updated. Is there a better way to return Bluebird promise from the service ?

EDIT: I wrapped my call like so.

getData(params) {
    // ... config, url
   return Promise.resolve( this.$http.get(url, config) )
       .then(result => {...})
       .catch((e) => {...})
}

This is returning me the needed bluebird promise and I am able to cancel it, but this fires up the angularjs $digest before I'm finished consuming the promise inside the controller, so the UI does not get updated. (I do not want to call a $scope.$apply)

knee pain
  • 600
  • 3
  • 19
  • How exactly did you wrap it? Show us that code because you should be able to wrap it in a `Promise.resolve()` just fine. – jfriend00 Jan 10 '18 at 17:10
  • 1
    There are several options you can choose from [here](https://stackoverflow.com/questions/13928057/how-to-cancel-an-http-request-in-angularjs) – HMR Jan 10 '18 at 17:55
  • @jfriend00 I provided the used code. – knee pain Jan 11 '18 at 07:42
  • @HMR , I actually ended up using something from the link you provided, thanks! – knee pain Jan 11 '18 at 10:35
  • I just realised that my question asking skills are not the best. I managed to let out the actual question when I wrote the title. – knee pain Jan 11 '18 at 10:36

0 Answers0