Return to site

ANGULAR TIP: How to deal with Promise LATE treatment completion

August 5, 2022

This week, a teammate had to handle a strange phenomenon ๐Ÿค” occurring while using an Angular App.

I remind you that Angular uses an asynchronous mechanism.

So, when you use the Angular App, for example clicking ๐Ÿ–ฑ๏ธ the menu, an event is thrown ๐Ÿš€ , then proceeded later.

Most of the time, everything is fine ๐Ÿ‘Œ.

The heck is when you are app handle some events very quickly ๐Ÿ’จ and so occur before expected ๐Ÿ˜ฎ.

So you may have a functional value of a previous call used instead of the one you are waiting for.

And you end with an incorrect โŒ GUI.

There are many ways ๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก of dealing with such a setting.

But when you deal with ๐๐ซ๐จ๐ฆ๐ข๐ฌ๐ž (https://lnkd.in/eJCwBNW2), you can just use the JS keyword ๐š๐ฐ๐š๐ข๐ญ before the call of your method returning a promise.

It will wait โฑ๏ธ for the promise to return its result. YAY! โ˜บ๏ธ

An outstanding, tremendous, snazzy gift from JavaScript. ๐Ÿ’ช

It made my day! ๐Ÿ™ (I was investigating a complex fix with Angular change detection)

 

Example from stackoverflow:

#angular #tip #angularProgramming #await