0

I have structured my AngularJs application in a way where I have a factory that fetch data from a back-end using ngResource as a dependency. This factory is injected in a Service, and finally this service is injected in a controller.

The problem is that since my Back-end calls take some time, results are not reflected in the controller. I do manage the promise in the service and it does return a result after some time 'cause I'm able to print it in the console, and I really want to avoid this promise handling in the controller, also the $q usage is now deprecated so I want to know if there's a more fancy solution more like Angular(2/4) that exists for AngularJs to resolve this problem.

Thank you

2
  • When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example. Commented Aug 2, 2017 at 1:07
  • Angular 2+ uses ES6 promises. AngularJS uses $q service promises which are integrated with the AngularJS framework and its digest cycle. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. I am sorry you don't like promises, but they are a reality of either framework. Commented Aug 2, 2017 at 1:28

1 Answer 1

1

I can't see another ways to use Promises (with angularjs $q) since it's in the core of AngularJS.

I found the doc explicit enough about this point : ngResource. However, ngResource is a wrapper of $http which give you a promise. In an other hand, ngResource provide you an easy way to bypass this step with a callback shortcut.

If you want a fancy solution in angularjs, you should stop usage of $resource / $http, and take a look at fetch and async await concepts.

Anothers solutions exists but it's some very old school ways to block your main thread.

NB: I'm very curious about the deprecation of $q and the fancy ways of Angular you quoted.

Sign up to request clarification or add additional context in comments.

2 Comments

Both the fetch API and the async/await constructs use ES6 promises which are not integrated with the AngularJS framework. On the other hand $q service promises are integrated with the AngularJS framework. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.
It makes sense now why they re-wrote the whole framework, I'm reaching other limits also for the factories based on ngResource. Thanks anyway your answer rocks

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.