Async and await confused me for weeks because every explanation started with the syntax and skipped the problem.
The problem is simple. Things like an API call or a database query take time, and if JavaScript waited for each one, the whole page would freeze.
So it starts the slow task, keeps running the rest of the code, and deals with the result when it arrives.
A Promise is just the name for that result that has not arrived yet. It ends up with data, or it fails with an error.
Before async and await we wrote long chains of then and catch, and the code read like a staircase.
With async and await you write it top to bottom like normal code, and wrap it in try and catch for errors. The word await only pauses that one function, not the page.
The mistake almost everyone makes is forgetting await, then printing the Promise and wondering why it says pending.
What worked for me was taking one old then chain from a project and rewriting it. It clicks fast after that.
If you are stuck on a case, post it here and I will try to help.
⚡SaudTech
Moderator · Tech Member
Building and running Hydcircle. Got feedback or found a bug?
Get in touch.