🚀 Angular async / await: Can You Use It? (OFFICIAL Confirmation)

Поделиться
HTML-код
  • Опубликовано: 29 дек 2024

Комментарии • 80

  • @AngularUniversity
    @AngularUniversity  9 месяцев назад +9

    Enjoy everyone, let me know if you plan on using async/await in your Angular applications?

  • @AleksandarT10
    @AleksandarT10 9 месяцев назад +8

    Great one, and i totally push for using async await as much as possible. Simpler and more readable code!

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +3

      Absolutely 🎉😊 It's such a breath of fresh air compared to other more complicated approaches, isn't it? But you wouldn't believe the amount of people that thinks that async/await doesn't even work in Angular, or that it shouldn't be used because of a variety of reasons. I'm very glad to hear that more people are adopting it 😉

  • @jonathangamble
    @jonathangamble 9 месяцев назад +4

    For loading before a component, it is important to note that async await should be in the resolver

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +2

      Yes it works on the resolver great, but notice that you can also call an async method from ngOnInit and have it load the data. In certain situations where we want to quickly load the screen and show something to the user this might be an option, but usually I agree a resolver is the best way to load the initial data. 👍

    • @jonathangamble
      @jonathangamble 9 месяцев назад

      @@AngularUniversity- ngOnInit will not wait for an async to finish even when you use async, you need to load it before the component... resolvers are the correct way in that case

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +3

      @@jonathangamble Correct it will not wait I know. The result of ngOnInit is ignored even if it's a promise, and calling a async method from ngOnInit will not block the process. But that is a good thing for what we want to do in certain cases. We just want the page to load ASAP and show multiple smaller loading local loading indicators inside the page, that each load certain data. Imagine a dashboard with multiple sections each with it's own loading indicator. you can do it either way, depending on the user experience that you are aiming for.👍

  • @RobertPattersonDev
    @RobertPattersonDev 9 месяцев назад +2

    Another great video Vasco! I love the async/await pattern (so much better than a chain of promises),

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      Thank you Robert, I'm happy to happy to hear that. 😊 Totally agree, for the most common every day scenarios, I think it's the right tool for the job 👍

  • @40tx
    @40tx 9 месяцев назад +1

    Thank you, thank you, thank you for getting the word out!

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      You're welcome, I'm very happy to share this and I hope the Angular community will consider different approaches to building applications, and async / await is an important ingredient for that 😊

  • @KeganVanSickle
    @KeganVanSickle 9 месяцев назад +2

    I've been using async/await in Angular for years. :D

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      Yes me too, it's crazy that this video is even necessary 😊 I got RxJs fans on Twitter telling me that it causes race conditions, writing posts saying that it doesn't work because it's not compatible with Zones. 🤣

  • @ico0z
    @ico0z 9 месяцев назад +3

    AsyncPipe supports promises as well, so there is nothing wrong using them instead of Observables

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      Correct, that is a feature that is not very well known, async pipe does support Promises 😊 Yes I agree, indeed there is nothing wrong with using them. 👍😉

    • @A_Dzzz
      @A_Dzzz 7 месяцев назад

      @@AngularUniversity So something that I'm wondering about AsyncPipe is, it uses "markForCheck" method, when a new value is emitted;
      So, if promises weren't changed to generators, or for an observable that reacts to something outside zone.js, AsyncPipe actually wouldn't trigger change detection, right?
      Which means it doesn't help regarding events happening outside zone.js/ngZone.

  • @JohanVrolix
    @JohanVrolix 9 месяцев назад +3

    Works with NX as well indeed if you are mindful of the babel plugin.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      I thought so too, thank you for confirming @Johan 😊, and is the plugin on by default in Nx, or do you have to configure it manually? With the Angular CLI i't's on by default, no special configuration is needed. 👍

    • @JohanVrolix
      @JohanVrolix 9 месяцев назад

      @@AngularUniversity hi Vasco, it’s on by default.

  • @KaliaH
    @KaliaH 9 месяцев назад

    For example, the Supabase documentation for Angular projects uses async/await heavily. It was something I had to get comfortable with. I did convert all to use rxjs, though, you'd probably tell me that is overkill now haha.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      If it works it works 😉But I encourage you to try other patterns for building Angular applications, RxJs is not the only way. 👍

  • @AntonioSantana-ll8il
    @AntonioSantana-ll8il 9 месяцев назад +1

    Excelllent video as always 🎉🎉🎉🎉

  • @AlainBoudard
    @AlainBoudard 9 месяцев назад

    Awesome résumé of this feature, and great talk with Mathieu 👍😎 Thanks Vasco
    And I agree with you about the next phases of Angular and zoneless, it will allow some native JavaScript uses like this one.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      Thank you Alain, I was very happy to see this confirmed too. 😉 Zoneless is going to be awesome, first though, signals:true 😉

  • @adamlawrence3651
    @adamlawrence3651 9 месяцев назад

    A lot of our code base is either ngrx or observable services, i think we could still utilise async await for Post/Put/Delete verbs when updating something on the back end. but retrieving data wiloukd atill use observables.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +2

      Hi Adam, I think that is very good start, async/await works especially well indeed for modification operations. 👍 For reads also works very well, you can read it and then emit it via an Observable is you are using RxJs, it's no problem. 👍 Or even save the data in a member variable and access it directly from the template, it all works great 😉

  • @juliancapponi4788
    @juliancapponi4788 6 месяцев назад

    Hello, very good video!
    Starting with angular 16, in the chrome debug, the "this" context inside an async function always turns out to be undefined. What is the reason?

  • @andyhb1970
    @andyhb1970 9 месяцев назад

    Although the async/await approach makes your code much easier to understand I'm trying to get my head around how you would implement a typical API call that requires behaviour that RXJS provides such as debounce, distinct and switchMap without needing to reinvent RXJS which seems to defeat the object?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      This is mentioned a lot, but the reality is that the vast majority of API calls don't need those features. You just make the call, get the data, handle an error and that's it. It's true that RxJs can be used for those cases, but if we don't need those features then why use RxJs? It's like hunting kittens with a tank 😉 The right tool for the job and minimizing unncessary complexity it's all i'm saying 👍

    • @andyhb1970
      @andyhb1970 9 месяцев назад

      @@AngularUniversity I like the "killing kittens with a tank" phrase 😂😂 OK I take your point so we simply use firstValueFrom to convert our Observable HTTP request to a promise and implement a boolean that manages whether a call is in progress to prevent multiple calls to the API due to an impatient user or a fast double click?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      @@andyhb1970 😉 Yes, as simple as that. 😊 You can create a global user indicator, or even turn it on via an HTTP interceptor so that you don't have to add those loading flags everywhere. Here is an implementation with an interceptor, you can even turn it off selectively - blog.angular-university.io/angular-loading-indicator/👍

  • @lukagojkovic4183
    @lukagojkovic4183 9 месяцев назад +1

    i thought you couldn’t use interceptors with async await for http requests? isn’t that a huge downside vs rxjs?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +2

      That is a very good question. 👍You can use the Angular HTTP Client with async / await simply by converting the returned Observable into a Promise using firstValueFrom(), check this video on more details ruclips.net/video/kTsVzAqX2wc/видео.html 👍 If you do this, HTTP interceptors will work great, it's what I do on my code bases lately. The code is very readable and simple, especially if you return Promises instead of Observables from your HTTP service layer. The APIs of Angular will be revisited in the near future to make RxJs optional, and this means that the HTTP client API will be one of the APIs to be reviewed. We don't know yet what it will look like, but meanwhile this solution works very well. 😊

  • @LarsRyeJeppesen
    @LarsRyeJeppesen 9 месяцев назад

    Using right now, works fine

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      Yes, it works great doesn't it? 😊 It's amazing so many people think it's broken by some reason. I've seen GDEs writing blog posts saying that it doesn't work. Some myths are so powerful 😂

  • @maxm1nd
    @maxm1nd 9 месяцев назад +1

    Why do you need async/await if there are rxjs and signals?

    • @SultanovAsrorbek
      @SultanovAsrorbek 9 месяцев назад +1

      Good question.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +3

      Great question, Signals is independent of the 2, signals track state. Async/await is a simplified way to handle async operations, very commonly used in other frameworks.RxJs will become optional in the future for Angular, so it's nice as a developer to be aware of alternatives, and async await is an important tool to learn as more projects are starting to use it. 👍

    • @AlainBoudard
      @AlainBoudard 9 месяцев назад

      It's always good to have multiple options to do different tasks. Async await is a tool, and no tool is supposed to answer all use cases.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      @@AlainBoudard I agree, the right tool for the job it's what its all about. 😉

    • @adambickford8720
      @adambickford8720 9 месяцев назад +1

      Simplicity. RxJs is actually a more powerful api (superset) but you often don't need that power, but you get the complexity either way.

  • @boris8983
    @boris8983 9 месяцев назад

    Does it make sense to use Promises AND Observables? I think you should use either one or the other but not both

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      That's a great question, I think so, that it does. A,d the argument of consistency doesn't hold water to me. I suggest to use promises and async /await for plain HTTP code that does not need any event orchestration. Just pulling data out of the database, saving, displaying an error. No need for RxJs for that, that's clear; For special cases that do need orchestration, only then use it; Those are few and far between, if they even occur at all for many applications. Let's use the right tool for the job, no need to go around hunting kittens with a tank. 😉

    • @boris8983
      @boris8983 9 месяцев назад

      ​@@AngularUniversity thank you for your answer. For me it is not (only) a matter of consistency but also how I handle and pass data throughout the application. Using Observables or now Signals works really well with a functional and declarative coding style, but I am not sure how it would look like with the async/await syntax.
      Also how does it work with onPush? You just mentioned that it works in the video
      PS: great image with the kitten and the tank :D

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      @@boris8983 There is nothing special about the syntax and OnPush, with OnPush you need to push the data to the component after fetching it from the backend via for example async/await. The code that we manage to write with RxJs is not really fully declarative, it's a mix between declarative and imperative. If you use async/await for most async operations and signals for reactivity, it will also be a mix between declarative and imperative, but just easier to read for most developers. If you don't know almost anything about async/await you will be able to read a code base written with it without any problem, that's it's big advantage.👍

  • @shanilkv
    @shanilkv 8 месяцев назад

    Thank you Vasco for your great explanation on this patterns. Can i ask one thing with you. Which method are you suggesting to use in real world application. async/await or Rxjs-Observable.?
    Looking forward to your advice on this. Thank you.!!

    • @AngularUniversity
      @AngularUniversity  8 месяцев назад +1

      Personally I have switched since over a year my whole team to to async/await and Promises, and we never looked back. We only use RxJs in places where we really need to, which is almost never, and things have never worked better. 👍

    • @shanilkv
      @shanilkv 8 месяцев назад

      @@AngularUniversity thank you so much for this advice.🫂🤝

  • @thedacian123
    @thedacian123 9 месяцев назад

    so ZOne js and zones are used by Angular for Change detections?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      Yes, Zones are used by default change detection, correct. 👍

  • @MustafaSerkanDokuzcan
    @MustafaSerkanDokuzcan 9 месяцев назад

    So what i understand the target should always

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      Hi Mustafa, actually no not really, you don't need to change the target you can keep it as it is. Instead, the CLI uses a plugin that ensures that async/await is converted into something else in production that is compatible with zones. So if you use the CLI, no action is needed; It seems like Nx supports it too. 😊

  • @VolodymyrShamieiev
    @VolodymyrShamieiev 8 месяцев назад

    And what about HTTP interceptors? :)

    • @AngularUniversity
      @AngularUniversity  8 месяцев назад +1

      You can still use the HTTP client with async/await, I will show that in some future video. 👍

  • @devfakeplus
    @devfakeplus 8 месяцев назад

    can you cancel ongoing http requests with async/await? with rxsj you can call switchMap on that.

    • @AngularUniversity
      @AngularUniversity  8 месяцев назад

      I know that is presented as an argument continuously. But it's not because RxJs is a good fit for making a search combobox that we need to write everything using it. In the vast majority of cases, there is nothing to cancel or orchestrate. We just pull data out of the database, show it in a table, save it back. Cancellation is an extremely rare use case. If really needed, yes you can do it with Promises and async/await too, or with a promise-based library like tRPC for example.

  • @nidinpereira5898
    @nidinpereira5898 9 месяцев назад

    Does it block the ui if the api is slow?
    Part of the beauty of using rxjs can have multiple components load different apis and that component shows up on the screen when they're done loading rather than waiting for everything to resolve

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +1

      Great question, it doesn't block the UI, but it can if you want to. You can either block the UI with a loading indicator, or do the request in the background and update the UI when done. But the async feature itself does not block anything, it's fully asynchronous. You can launch multiple requests in parallel with Promise.all(). It's a tradeoff when compared to RxJs. Async /await does very well the typical everyday use cases, RxJs is good for much more advanced scenarios, it's a matter of using the right tool for the job. 👍 Personally for every day HTTP operations like pulling data from a backend and showing it to the user, my team has been using async await for a while now and with great results, the code is way simpler to read and I get way less review issues, I need to give much less developer support, etc. 😊

    • @seanszarkowicz
      @seanszarkowicz 9 месяцев назад

      @@AngularUniversity do you have plans to do a real basic video on using async / await with native FETCH as well as using interceptors ?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      @@seanszarkowicz I did a video showing how to use with the Angular HTTP client. I could do it but there is nothing much to show. I might still do it I've added it to the list, thank you for the idea. 👍

  • @rishimondal3010
    @rishimondal3010 9 месяцев назад

    Can you make a video on angular signal store and firebase

  • @felexonyango-lf8ev
    @felexonyango-lf8ev 9 месяцев назад

    Could you share that PR please ?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      No problem, it's this one, I will add it to the description too 😊 github.com/stackblitz/starters/pull/24

  • @sergey13
    @sergey13 9 месяцев назад

    So, it's ok to use async/await if you're using cli, and what about nx?

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад

      Great question, I personally use the CLI, but I bet Nx also has full support for async/await, and it either uses internally the exact same bable plugin, or something very similar. If you find confirmation, please share it here in the comments, but I cannot imagine that it would be otherwise. 😉

    • @JohanVrolix
      @JohanVrolix 9 месяцев назад

      @@AngularUniversityI can confirm it works with NX

    • @MustafaSerkanDokuzcan
      @MustafaSerkanDokuzcan 9 месяцев назад

      Ad i know nx use also the ng cli to build the angular application. Nx cli is as big wrapper and will delegate to the right cli

  • @SultanovAsrorbek
    @SultanovAsrorbek 9 месяцев назад +1

    🎉🎉

  • @TheRealLeccho
    @TheRealLeccho 9 месяцев назад +4

    Sure let's use async/await and bring back callback hell... RxJs is just better.

    • @AngularUniversity
      @AngularUniversity  9 месяцев назад +5

      Async / await solves callback hell very elegantly. It reads just like synchronous code. Your code remains flat. RxJs is still a good fit for certain use cases, it's not going away. But it will become optional, so I think it's important as an Angular developer to also know async/await, as more and more projects are starting to use it.

    • @TheRealLeccho
      @TheRealLeccho 9 месяцев назад +2

      ​@@AngularUniversity You can create excellent code using async/await, but it's questionable whether someone who isn't proficient in RxJs would utilize async/await effectively. This could lead to callback hell and potentially excessive individual awaits instead of properly grouping them. Employing RxJs not only enhances code readability but also mitigates the risk of encountering these issues.

    • @yufgyug3735
      @yufgyug3735 9 месяцев назад +2

      in most languages, async/await solves the problem of callback hell, thus making the code simpler and easier to read. same here. can you misuse it? sure, just like any other tool, be it promises or rxjs.

    • @AntonioSantana-ll8il
      @AntonioSantana-ll8il 9 месяцев назад

      I'm agree that Rxjs is Nice, but in some cases, have 3 switchMap and 5 operators in one Pipe is not the better options, async/await helps in that cases, @AngularUniversity has an excellent video regarding that subject, i recommend you to watch it

    • @PlerbyMcFlerb
      @PlerbyMcFlerb 9 месяцев назад

      I feel the view of callback hell just referring to the level of nesting and indentation is missing the primary issue, which is uncoordinated asynchrony, ie race conditions. It is a more fundamental issue than just 'readability.' In the wrong hands async await can lead to uncoordinated buggy inscrutible spaghetti
      Granted it does ultimately boil down to a skill issue and I love async await for many tasks.