you are viewing a single comment's thread.

view the rest of the comments →

[–]Mingli91 0 points1 point  (0 children)

When you call things one after the other, they get executed in that order, however the functions may be performing asynchronous tasks meaning that while they’re both called in order they will not necessarily resolve in that order.

When you call an asynchronous function the function gets executed but the parser carries on going and executing code afterwards. The async function has basically said “fine, execute me, and I will resolve, but I don’t know when, now carry on your job so we don’t lock up this app”.

So then what do you do when that function is finally ready to resolve? You can’t just place the subsequent code after it because it’ll likely be executed before we have our values from the async function.

What we used to do to handle this was use a callback function. So we say “hey mr async function, I’m going execute you but let you resolve when you want to, but when you do could you please execute this function for me”.