0

Basically, I am building a massive app, using SailsJS and AngularJS. I'm ratter new to both of them.

I've started this app with few Controllers, but seeing how it's getting messy, I started to move the interaction with my Backend API into Services (mostly the $http functions). I quickly noticed that the "asynchonousity" of it messed most of my logic.

I never used the $q of Angular, and $http kind of do it by itself so I looked online to see the best practice and found this post : AngularJS services and promises best practice

As it's from 2016, I was wondering if it is still considered the best practice, and if it is exactly like that that I should build my Services.

StS
  • 49
  • 1
  • 11
  • The API hasn't changed since then. But the service should at least abstract away from$http, and returns a Promise of data rather than a promise of http response. Read http://blog.ninja-squad.com/2015/05/28/angularjs-promises/ (and the followup article). Note that it's quite strange to start a new project with AngularJS, which is now in final maintenance mode, and won't evolve anymore. – JB Nizet Apr 13 '18 at 17:10
  • Thank you for pointing it out ! I know but apparently it works great with SailsJS. We thought of upgrading but would have been more trouble... – StS Apr 13 '18 at 17:26
  • The thing that has changed is the arrival of async/await. You should have a good knowledge of how the framework, native promises and async/await work, because async/await requires additional steps to be integrated properly with digest cycles. See for some ideas, https://stackoverflow.com/a/45119519/3731501 – Estus Flask Apr 13 '18 at 17:54

1 Answers1

0

It still is considered the best practice. You can use Promise Chaining in order to structure your code timewise.

You may want to see this article for further details.

Sajjad Shahi
  • 572
  • 4
  • 14