Menu

1 / 2

JS async and await

DEV Community·Nanthini Ammu·6 months ago
#ODZpMQuF
#beginners#javascript#tutorial#webdev#resolve
Reading 0:00
15s threshold

What is async and await?

async and await are used to handle asynchronous operations in a cleaner way.

Why Do We Need Asynchronous Code?

JavaScript is single-threaded — it can only do one thing at a time. But many real-world tasks (fetching data from a server, reading a file, waiting for a timer) take time. If JS waited for each of these to finish before moving on, the browser would freeze and become unresponsive.

Asynchronous programming allows JS to start a slow task, continue running other code, and then come back to handle the result when it's ready — without blocking the main thread.

Fetching data from an API (network request)

Reading/writing files (Node.js)

setTimeout / setInterval (timers)

Database queries

Evolution : Callbacks → Promises → Async/Await :

Callbacks(Old way) :

setTimeout (() => { console . log ( " Prepping " ); setTimeout (() => { console . log ( " Cooking " ); setTimeout (() => { console .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.