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)

5
  • How exactly did you wrap it? Show us that code because you should be able to wrap it in a Promise.resolve() just fine. Commented Jan 10, 2018 at 17:10
  • 1
    There are several options you can choose from here Commented Jan 10, 2018 at 17:55
  • @jfriend00 I provided the used code. Commented Jan 11, 2018 at 7:42
  • @HMR , I actually ended up using something from the link you provided, thanks! Commented Jan 11, 2018 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. Commented Jan 11, 2018 at 10:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.