JavaScript Async / Await

JavaScript Async / Await

async and await are modern JavaScript features that make working with Promises easier and code more readable, avoiding the “callback hell.”


1️⃣ Async Functions

  • An async function always returns a Promise.

  • If a function returns a value, it is wrapped in a resolved Promise.

  • You can use await only inside async functions.


 


2️⃣ Await Keyword

  • await pauses the execution of the async function until the Promise resolves.

  • It can only be used inside async functions.


 


3️⃣ Error Handling with Try / Catch


 


4️⃣ Sequential vs Parallel Execution

Sequential Execution (Slower)



 

Parallel Execution (Faster)



 


5️⃣ Real World Example: Fetch API


 


6️⃣ Key Rules

  1. Always use await inside async functions.

  2. async functions return a Promise.

  3. Use try/catch to handle errors.

  4. Combine with Promise.all() for parallel execution.


⭐ Summary Table

Feature Description
async Declares an asynchronous function
await Waits for a Promise to resolve
Error Handling Use try / catch
Return Value Always a Promise
Sequential Execution await one by one
Parallel Execution Promise.all([...])

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *