0
Well, I work with angular nowadays and I always wonder if it is wrong to use Promises or not, example. I have a code that needs to be async. First function must validate the access permission, after validating the status of the employee and going through these validations must make the call to load the information on the screen... something of this type
ngOnInit() {
if (this.validatePermission() && this.validateStatus('ACTIVE')) {
this.showUserDetail(100);
}
}
To make every function of this return a precedent seems wrong to me...
What would be the best way to do that, would be Observables ?
Why would it be wrong to use promises? There’s nothing wrong with them. It depends on what you need to do. Promises and observables are not fully equivalent, so that one should not be "exchanged" for the other without due criterion.
– Luiz Felipe
Work with Angular about 3 years and never seen in Angular projects no use of Promise pure (Vanilla), for it exists Observable and even the toPromysis that are from the bilbioteca
rxjs
, pq should use it pure? Pq these Features do not suit your project?Fazer com que cada função dessa retorne uma promise me parece errado...
, Why do you think that? If these functions tend to fetch data on some server, how do you want them not to be asynchronous (Promises or Observables)??– LeAndrade
In my case using async/await would be the easiest way to do this, but I know there are functions in rxjs like mergeMap where I could get the same result. Whenever I talk about using Predomise around here at the company comes up this subject that Promise was disparaged, but I never found it anywhere... Only encounter that Promise was replaced by Observable on Httpclinte. My doubt here was just about it, if anyone else has this view that we should avoid using Promise and prefer observable, but from what I’m seeing is a wrong opinion.
– Maurício Júnior
There’s no such thing as
Promise
is depreciated. Depreciation is something else. Maybe not using promises is a company convention, although I would question it. Just out of curiosity,async
/await
can almost be seen as a syntactic sugar to "unpack" promises (learn more). Therefore,async
/await
based on promises - if promises were belittled,async
/await
would therefore also be depreciated.– Luiz Felipe
The standard of angular is to use observables if your team uses it instead of Promises I think it’s a good idea to follow
– Eduardo Vargas