Hi everyone π,
When I first saw async and await in a JavaScript tutorial on YouTube, I was confused β even the names felt strange π
.
I kept wondering, what is this and why do we even need it?
But slowly, when I started using async and await in real code, I understood their purpose.
They are mainly used while calling APIs. When an API request is made, async and await make sure the code does not get stuck while waiting for the response.
The await keyword waits for the API result and then returns the actual response data once it is ready.
To handle possible errors during API calls, we usually use tryβcatch with async/await.
This helps prevent the application from crashing if something goes wrong, like a network issue or server error.
async function getTodos() {
try {
const res = await fetch("/todos");
const data = await res.json();
console.log(data);
} catch (error) {
console.log("Error while fetching todos");
}
}
One important thing I realized during this time is that practice matters the most.
No matter how many tutorials we watch, a new concept only starts making sense when we actually use it in our own code.
Thatβs when it becomes part of our muscle memory and feels natural to use.
Thanks for reading β€οΈ
Top comments (1)
nice read, keep practicing and evolving. I like how you write articles first yourself and then use AI to refine them, nice approach