Reputation: 795
Javascript a async language but on a lot of sites I see people write javascript code in a specific order.
The picture below gives an example:
When I use this kind of code I am always afraid that the setItem is performed before the modification/update is done.
How do I know if this code run as intended, and if this code is wrong what is the right way to fix those situations?
Upvotes: 0
Views: 42
Reputation: 1532
That's not what is meant by async. Javascript will execute that code from top to bottom always.
By async, people mean, that sometimes there will be a function, that makes for example an HTTP request. It's unknown when the data will come back, so as far I understand it, the engine will continue executing other code.
That's why people use callbacks, when that request is done, browser knows to call back some function (if you provided one of course), when the data is ready.
Other than cases like this, you shouldn't worry about localStorages.setItem firing off before your for loop is finished.
Upvotes: 2